I have this kind of an array:-
$result=Array([city] => xyz [name] => Array( [0] => Xyz1 [1] => xyz2 ) [email] => Array ( [0] => xyz1#gmail.com [1] => xyz2#gmail.com ) [phone] => Array ( [0] => 23423423-1 [1] => 23423423-2 ) [address] => Array ( [0] => xyz1 [1] => xyz2 ) [dis] => xyz);
and i want this like
$r1=Array ( [city] => xyz [name] => xyz1 [email] => xyz1#gmail.com [phone] => 23423423-1 [address] => xyz1 [dis] => xyz );
$r2=Array ([city] => xyz [name] => xyz2 [email] => xyz2#gmail.com [phone] => 23423423-2 [address] => xyz2 [city] => xyz );
This assumes that you will have more than one array to process and simply sorts your elements into a new, formatted array.
<?php
$results = array(
array(
'city' => 'xyz',
'name' => array( 'Xyz1', 'xyz2' ),
'email' => array( 'xyz1#gmail.com', 'xyz2#gmail.com' ),
'phone' => array( '23423423-1', '23423423-2' ),
'address' => array( 'xyz1', 'xyz2' ),
'dis' => 'xyz'
)
);
$formatted_results = array();
foreach( $results as $result ) {
foreach( array(0,1) as $key ) {
$formatted_results[][$key] = array(
'city' => $result['city'],
'name' => $result['name'][$key],
'email' => $result['email'][$key],
'phone' => $result['phone'][$key],
'address' => $result['address'][$key],
'dis' => $result['dis']
);
}
}
echo "<pre>";
print_r($formatted_results);
This will output
Array
(
[0] => Array
(
[0] => Array
(
[city] => xyz
[name] => Xyz1
[email] => xyz1#gmail.com
[phone] => 23423423-1
[address] => xyz1
[dis] => xyz
)
)
[1] => Array
(
[1] => Array
(
[city] => xyz
[name] => xyz2
[email] => xyz2#gmail.com
[phone] => 23423423-2
[address] => xyz2
[dis] => xyz
)
)
)
You can then access your new values like this:
foreach( $formatted_results as $value ) {
$r1 = $value[0];
$r2 = $value[1];
}
Or if you only have the single array:
$r1 = $formatted_results[0][0];
$r2 = $formatted_results[0][1];
Related
i have two separate arrays $first_one:
Array
(
[0] => Array
(
[_date] => 2019-10-16
[_number] => 1
[_order] => 1
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[_date] => 2020-10-11
[_number] => 2
[_order] => 2
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)
and the $second_array:
Array
(
[0] => Array
(
[date] => 2019-10-16
[number] => 1
[order] => 1
[name] => jack
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[date] => 2020-10-11
[number] => 2
[order] => 2
[name] => joey
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1433
)
)
[2] => Array
(
[date] => 2020-10-28
[number] => 3
[order] => 3
[name] => tom
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1593
)
)
)
they are very similar but they came from different api's and they are different in numbers and also some key names.
so what i'm trying to do is to count the $first_one arrays and if for that many loop through the $second_one (in this example is 2) and if the [_number] [_order] from $first_one was equal (==) to [number] [number] $second_one then take some info (for example the last name) from it and put in a new array.
so is this possible to do?
$arr1 = [ [ '_date' => '2019-10-16','_number' => 1,'_order' => 1,
'name' => 'jack','other_ids' => ['b_id' => 1253]
],
['_date' => 2020-10-11,'_number' => 2,'_order' => 2,
'name' => 'joey','other_ids' => ['b_id' => 1433]
]
];
$arr2 = [ [ 'date' => '2019-10-16','number' => '1','order' => '1',
'name' => 'jack','last_name' => 'foobar','other_ids' => ['b_id' => 1253]
],
[ 'date' => '2019-10-11','number' => '2','order' => '2',
'name' => 'joey','last_name' => 'foobar','other_ids' => ['b_id' => 1433]
],
[ 'date' => '2019-10-28', 'number' => '3', 'order' => '3',
'name' => 'tom', 'last_name' => 'foobar', 'other_ids' => ['b_id' => 1593]
],
];
// first make second array more directly searchable
// make new array with the `number` as the key
foreach( $arr2 as $a){
$arr2new[$a['number']] = $a;
}
foreach ($arr1 as $a) {
if ( array_key_exists($a['_number'], $arr2new) &&
$a['_order'] == $arr2new[$a['_order']]['order'] )
{
$merged[] = ['name'=>$a['name'], 'other_ids' => $a['other_ids']];
}
}
print_r($merged);
RESULT
Array
(
[0] => Array (
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array (
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)
Assuming I have an array and recursively want to do some task. I have spent one day for this unable to solve this. May be my logic is not good.
Any help on how to do such thing in an efficient way would save my days.
I have an array with n level deep, looking like:
Array
(
[0] => Array
(
[name] => user1
[email] => user1#demo.com
[depth] => 1
)
[1] => Array
(
[name] => user2
[email] => user2#demo.com
[depth] => 1
[0] => Array
(
[0] => Array
(
[name] => user2.1
[email] => user2.1#demo.com
[depth] => 2
)
)
)
[2] => Array
(
[name] => user3
[email] => user3#demo.com
[depth] => 1
[0] => Array
(
[0] => Array
(
[name] => user3.1
[email] => user3.1#demo.com
[depth] => 2
[0] => Array
(
[0] => Array
(
[name] => user3.1.1
[email] => user3.1.1#demo.com
[depth] => 3
)
)
)
[1] => Array
(
[name] => user3.2
[email] => user3.2#demo.com
[depth] => 2
)
)
)
)
I want to change above array in exactly this format:
array(
0 => array(
'name' => 'user1',
),
1 => array(
'name' => 'user2',
'children' => array(
0 => array(
'name' => 'user2.1',
) ,
) ,
) ,
2 => array(
'name' => 'user3',
'children' => array(
0 => array(
'name' => 'user3.1',
'children' => array(
0 => array(
'name' => 'user3.1.1',
) ,
) ,
) ,
1 => array(
'name' => '3.2',
)
) ,
) ,
)
Edited:
I am using this code and working fine if i want to show data in tree format but unable to push data into array as i want.
function displayArrayRecursively($arr, $indent='') {
if ($arr) {
foreach ($arr as $key => $value) {
if (is_array($value)) {
displayArrayRecursively($value, $indent . '-->');
} else {
if ($key == 'name')
{
echo "$indent $value <br>";
}
else {
continue;
}
}
}
}
}
displayArrayRecursively($userListArray);
Can anyone suggest me how to do this?
Thank you
This function will do what you want:
function process_nodes($nodes) {
$new = array();
foreach ($nodes as $node) {
$new[] = array('name' => $node['name']);
if (isset($node[0])) {
$new[count($new)-1]['children'] = process_nodes($node[0]);
}
}
return $new;
}
print_r(process_nodes($data));
Output:
Array
(
[0] => Array
(
[name] => user1
)
[1] => Array
(
[name] => user2
[children] => Array
(
[0] => Array
(
[name] => user2.1
)
)
)
[2] => Array
(
[name] => user3
[children] => Array
(
[0] => Array
(
[name] => user3.1
[children] => Array
(
[0] => Array
(
[name] => user3.1.1
)
)
)
[1] => Array
(
[name] => user3.2
)
)
)
)
//for below array i need the values of name to be semi-colon separated
output should be
monica;pradnesh
Array
(
[0] => Array
(
[0] => Array
(
[name] => Monica
[address] => Surat
[mobile_number] => 8956231245
[telephone_number] =>
[email_id] => monica#kritva.com
[DOB] => 0000-00-00
[gender] => female
[PAN_number] => ASDFG4567A
[customer_type] => SALES
[dependency_type] => Retail
[cust_id] => 9055954
[state] => Gujarat
[city] => Surat
[zipcode] => 752852
[exist_from] => 2016-12-20
[edit_date] => 0000-00-00
[staff_id] =>
[BA_id] =>
[id] => 31
[nationality] => Indian
)
)
[1] => Array
(
[0] => Array
(
[name] => Pradnesh
[address] => Surat
[mobile_number] => 8956231245
[telephone_number] =>
[email_id] => pradnesh.valapkar#kritva.com
[DOB] => 0000-00-00
[gender] => male
[PAN_number] => GHJKL9876S
[customer_type] => NRI
[dependency_type] => BA
[cust_id] => 2736738
[state] => Gujarat
[city] => Surat
[zipcode] => 895623
[exist_from] => 2016-12-21
[edit_date] => 0000-00-00
[staff_id] =>
[BA_id] => 5822043
[id] => 33
[nationality] => Indian
)
)
)
first extract all the name to array $names, then use implode like this:
$names = array_map(function($v){return $v[0]['name'];}, $array);
implode(',', $names);
Might be this can show you direction.
<?php
$data = array(
array(
array(
'name' => 'Monica',
'address' => 'surat'
),
array(
'name' => 'Priya',
'address' => 'surat'
)
),
array(
array(
'name' => 'Pradnesh',
'address' => 'surat'
),
array(
'name' => 'test',
'address' => 'surat'
)
)
);
array_map(function ($entry) {
echo implode(';',array_map(function ($d) {
return $d['name'];
}, $entry));
}, $data);
?>
Output
Monica;PriyaPradnesh;test
i have array and i need to reorder by another array:
here is my array:
Array
(
[0] => Array
(
[name] => Haroldas
[number] => 444
[address] => g.
[city] => eee
[country] => f
[lastname] => r
)
[1] => Array
(
[name] => Lukas
[number] => 999
[address] => rrr
[city] => tttt
[country] => 3
[lastname] => r
)
)
This is another array with columns number which show me how to order number (columns order can to be another):
Array
(
[lastname] => 4
[name] => 1
[number] => 5
[address] => 3
[city] => 0
[country] => 2
)
i need result like this:
Array
(
[0] => Array
(
[city] => eee
[name] => Haroldas
[country] => f
[address] => g.
[lastname] => r
[number] => 444
)
...
)
This should do the job:
<?php
$order = [
'city' => 0,
'name' => 1,
'country' => 2,
'address' => 3,
'lastname' => 4,
'number' => 5,
];
$data = [
[
'name' => 'Haroldas',
'number' => '444',
'address' => 'g.',
'city' => 'eee',
'country' => 'f',
'lastname' => 'r',
],
[
'name' => 'Lukas',
'number' => '999',
'address' => 'rrr',
'city' => 'ttt',
'country' => 3,
'lastname' => 'r',
],
];
foreach ($data as &$entry) {
uksort($entry, function ($a, $b) use ($order) {
return strcmp($order[$a], $order[$b]);
});
}
print_r($data);
The output is:
Array
(
[0] => Array
(
[city] => eee
[name] => Haroldas
[country] => f
[address] => g.
[lastname] => r
[number] => 444
)
[1] => Array
(
[city] => ttt
[name] => Lukas
[country] => 3
[address] => rrr
[lastname] => r
[number] => 999
)
)
If you want to reorder that use foreach then create a new array.
$arrReorder = [];
foreach($yourArr as $key => $val){
$arrReorder[] = ['city' => $val['city'], 'name' => $val['name'], 'country' => $val['country'], 'address' => $val['address'], 'lastname' => $val['lastname'], 'number' => $val['number'] ];
}
print_r($arrReorder);
This is my array.
Array
(
[id] => 1
[color] => "White"
[something] => Array
(
[country] => "France"
[city] => "Paris"
)
)
Array
(
[id] => 2
[color] => "Black"
[something] => Array
(
[country] => "Germany"
[city] => "Berlin"
)
)
Array
(
[id] => 2
[color] => "Red"
[something] => Array
(
[country] => "Russia"
[city] => "Moscow"
)
)
I want to group arrays with same id value. This should be the output:
[0] => Array
(
[0] => Array
(
[id] => 1
[color] => "White"
[something] => Array
(
[country] => "France"
[city] => "Paris"
)
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[color] => "Black"
[something] => Array
(
[country] => "Germany"
[city] => "Berlin"
)
)
[1] => Array
(
[id] => 2
[color] => "Red"
[something] => Array
(
[country] => "Russia"
[city] => "Moscow"
)
)
)
I tried with tens of foreach statements but there's no way for me to get arrays with same id inside the same array. Is it probably related with the fact that it's a multidimensional array? Should I use 2 nested foreach to get the result?
Code:
<?php
$arr = array(
array(
'id' => 1,
'color' => 'white',
'something' => array(
'country' => 'France',
'city' => 'Paris',
),
),
array(
'id' => 2,
'color' => 'Black',
'something' => array(
'country' => 'Germany',
'city' => 'Berlin',
),
),
array(
'id' => 2,
'color' => 'Red',
'something' => array(
'country' => 'Russia',
'city' => 'Moscow',
),
),
);
function groupify($arr) {
$new = array();
foreach ($arr as $item) {
if (!isset($new[$item['id']])) {
$new[$item['id']] = array();
}
$new[$item['id']][] = $item;
}
return $new;
}
print_r(groupify($arr));
Result:
Array
(
[1] => Array
(
[0] => Array
(
[id] => 1
[color] => white
[something] => Array
(
[country] => France
[city] => Paris
)
)
)
[2] => Array
(
[0] => Array
(
[id] => 2
[color] => Black
[something] => Array
(
[country] => Germany
[city] => Berlin
)
)
[1] => Array
(
[id] => 2
[color] => Red
[something] => Array
(
[country] => Russia
[city] => Moscow
)
)
)
)
if you don't want to preserve keys, just call array_values before return.
Use id for key of new array.
$a[$array[id]][] = $array;
If you wanted to use a foreach:
<?php
$return = array();
foreach($array as $key => $innerArray) {
$return[$innerArray['id']][]= $innerArray;
}
Now $return contains them groped by ID, where keys 1 and 2 are your IDs
array(
1 => array(
array(/** **/)
),
2 => array(
array(/** **/),
array(/** **/)
);
);
You can then access your groups like this:
foreach($return as $key => $groupArray) {
// you have the groups here
foreach($groupArray as $id => $singleArray) {
// singleArray contains your id, colour etc
}
}
foreach($return[1] as $groupOne) {
// all arrays with id = 1
}