views:

394

answers:

1
<?php
$frndof=array("name"=>'frnd_of_xyx',"id"=>001,"url"=>'tutorboy.com/frnd_of_xyx',
"address"=>array("city"=>'NewZend',"zip"=>'100450'));
$frndof1=array("name"=>'frnd_of_xyx1',"id"=>0011,"url"=>'tutorboy.com/frnd_of_xyx1',
"frnds"=>$frndof,"address"=>array("city"=>'NewZend1',"zip"=>'100422'));
$a = array( array("name"=>'xyx',"id"=>001,"url"=>'tutorboy.com/xyx',
"address"=>array("city"=>'cochin',"zip"=>'100450')),
        array("name"=>'abc',"id"=>001,"url"=>'tutorboy.com/abc',"frnds"=>$frndof1,"address"=>array("city"=>'Bosco',"zip"=>'100450')),
        array("name"=>'EFG',"id"=>001,"url"=>'tutorboy.com/EFG',"address"=>array("city"=>'NY',"zip"=>'100450')) 
       ); 

?>
<pre>
<?
 print_r($a);
?>



Array
(
[0] => Array
    (
        [name] => xyx
        [id] => 1
        [url] =>example.com/xyx
        [address] => Array
            (
                [city] => cochin
                [zip] => 100450
            )

    )

[1] => Array
    (
        [name] => abc
        [id] => 1
        [url] =>example.com/abc
        [frnds] => Array
            (
                [name] => frnd_of_xyx1
                [id] => 9
                [url] =>example.com/frnd_of_xyx1
                [frnds] => Array
                    (
                        [name] => frnd_of_xyx
                        [id] => 1
                        [url] =>example.com/frnd_of_xyx
                        [address] => Array
                            (
                                [city] => NewZend
                                [zip] => 100450
                            )

                    )

                [address] => Array
                    (
                        [city] => NewZend1
                        [zip] => 100422
                    )

            )

        [address] => Array
            (
                [city] => Bosco
                [zip] => 100450
            )

    )

[2] => Array
    (
        [name] => EFG
        [id] => 1
        [url] =>example.com/EFG
        [address] => Array
            (
                [city] => NY
                [zip] => 100450
            )

    )

)

This is my array. :-)

I need a to replace the following array array_fig(1) with

[frnds] => Array
                (
                    [name] => frnd_of_xyx
                    [id] => 1
                    [url] =>example.com/frnd_of_xyx /* this is the unique key */
                    [address] => Array
                        (
                            [city] => NewZend
                            [zip] => 100450
                        )

                )

array_fig(2)

[frnds] => Array
                (
                    [name] => frnd_of_xyx
                    [id] => 1
                    [url] =>example.com/frnd_of_xyx /* this is the unique key */
                    [address] => Array
                        (
                            [city] => NewZend_neww
                            [zip] => 1004501212122
                        )

                )

The Key URL is the Unique key. [frnds] array need to be repalce with new [frnds] array. ie How to find the parent key or array key with " [url] " key as the unique.

Note:this array is used for the xml generation so, these are the elements and arrtibutes, so inner elements concepts

A: 

As you have not used the key field as actual key in your array, the only available technique is linear search, i.e. iterate over the whole array with foreach.

$found=false;
foreach ($myarray as $key=>$val)
{
    if ($val['url']==$targetURL)
    {
        $myarray[$key]['address']=$newAddress;
        $found=true;
        break; 
    }
}

In Javascript, something like this

   var found=false;
   var key;
   for (key in myarray)
   {
       if (myarray[key]['url']==targetURL)
       {
             myarray[key]['address']=newAddress;
             found=true;
             break;
       }
   }
Paul Dixon
is this possible in Javascript?
coderex
@Paul Dixon: This array is used for xml generation.
coderex
@Paul Dixon: now it s replacing the address, but i need to replace the complete [frnds] array which having the url
coderex
My example shows how to loop an object, and how to access its members - you should be able to take it from here!
Paul Dixon