Hi,
any one please help to convert this first array to second array
First Array:-
Array
(
[items] => Array
(
[0] => Array
(
[Id] => 7
[Name] => server1
)
[1] => Array
(
[Id] => 17
[Name] => Test1
)
)
);
Second Array :-
Array(
‘items’ => Array
(
‘0’ => Array
(
‘Id’ => ‘7’,
‘Name’ => ‘server2’
),
'1' => Array
(
'Id' => '17',
'Name' => 'Test2'
)
)
);
oh, i didn’t know this about javascript. thnx
nishu123, do you really want the output converting like that (I only ask because it seems an odd request to change the data and output in that strange format)? In which case, can you show us how you are getting the original output (it looks like from print_r )?
Thankyou dear.
My first array item is displayed as [items] and In second array displayed as ‘items’. can we replace in to ‘’ in whole array?
please…
It’ll be interesting to see what nishu123 makes of this.
RNEL
August 12, 2010, 9:58am
6
when you assign a new value to an array you are already replacing it
$x['items'][$i][$key] = preg_replace('/'.$x['items'][$i][$key].'/i', $y['items'][$i][$key], $x['items'][$i][$key]);
and
$x['items'][$i][$key] = $y['items'][$i][$key];
both returns
Array ( [0] => Array ( [id] => 7 [name] => server2 ) [1] => Array ( [id] => 17 [name] => test2 ) )
which makes the task accomplished
nishu123:
hi,
I worte a function it will return JSON. I will decode this by using json_decode, so i will get the result like First Array. am i right? But I need the array format like second Array. ie, [0] to ‘0’ and [items] to ‘items’ like that.
With JavaScript, it doesn’t matter if numeric indexes are referenced as integers or as strings.
For example:
var arr = [];
arr[4] = 'item 1';
arr['5'] = 'item 2';
Both arr[4] and arr[‘4’] can be used to access ‘item 1’ , and similarly, arr[5] and arr[‘5’] will access ‘item 2’
hi,
I worte a function it will return JSON. I will decode this by using json_decode, so i will get the result like First Array. am i right? But I need the array format like second Array. ie, [0] to ‘0’ and [items] to ‘items’ like that.
RNEL:
task accomplished
Unfortunately it was using assignment, rather than a string replacement as was hinted at by the subject line: replace string in an array
Perhaps a look at the documentation for preg_replace will be of some help.
smftre
August 11, 2010, 3:18pm
10
Can you provide a little more information as to what you are trying to accomplish?
RNEL
August 12, 2010, 7:50am
11
$x is your first array
$y is your second array
$x = array(
'items' => array(
0 => array(id => '7', name => 'server1'),
1 => array(id => '17', name => 'test1')
),
);
$y = array(
'items' => array(
0 => array(id => '7', name => 'server2'),
1 => array(id => '17', name => 'test2')
),
);
first let’s check what’s inside your arrays
print_r($x['items']);
echo '<br />';
print_r($y['items']);
returns
Array ( [0] => Array ( [id] => 7 [name] => server1 ) [1] => Array ( [id] => 17 [name] => test1 ) )
Array ( [0] => Array ( [id] => 7 [name] => server2 ) [1] => Array ( [id] => 17 [name] => test2 ) )
now let’s perform your task - convert first array to second array
for($i=0; $i<count($x['items'][0]); $i++){
foreach($x['items'][$i] AS $key => $value){
$x['items'][$i][$key] = $y['items'][$i][$key];
}
}
let’s check what are the new values of your arrays
print_r($x['items']);
echo '<br />';
print_r($y['items']);
returns
Array ( [0] => Array ( [id] => 7 [name] => server2 ) [1] => Array ( [id] => 17 [name] => test2 ) )
Array ( [0] => Array ( [id] => 7 [name] => server2 ) [1] => Array ( [id] => 17 [name] => test2 ) )
task accomplished