I have this kind of array which I want it to be populated with values from MySQL db. For example if there are 5 rows in the database, it should loop the array 5 times together with the values in it.
This is how my current array (to be parsed in JSON) looks like:
array(
1 => array(
"id" => 1,
"category" => "For men",
"items" => array(
array(
"id" => 1,
"name" => "Baju",
"price" => "5:52"
),
array(
"id" => 2,
"name" => "The Canyon",
"price" => "3:01"
)
)
),
2 => array(
"id" => 2,
"category" => "Adele 21",
"items" => array(
array(
"id" => 1,
"name" => "Rolling In The Deep",
"price" => "03:48"
),
array(
"id" => 2,
"name" => "Rumour Has It",
"price" => "03:43"
)
)
)
),
.
.
.
. //more arrays here;
So I did a while loop to print out (print_r()) the array according from my database. It looks like this:
Array (
[0] => Array (
[id] => 1
[item_photo] => gambar1
[item_name] => Kotak
[item_description] => Desc 1 - Lorem ipsum blablabla
[user_id] => 6
[category_id] => 2
)
[1] => Array (
[id] => 2
[item_photo] => gambar2
[item_name] => Botol
[item_description] => Desc 2 - Lorem ipsum blablabla
[user_id] => 6
[category_id] => 3
)
[n] . . .
.
.
.
)
What I wanna achieve is to replace the values in the first array (to be parsed in JSON) with the one in array two. Like so,
array(
$category_id => array(
"id" => $i++,
"category" => $category_name,
"items" => array(
array(
"id" => $j++,
"user_name" => $user_name,
"name" => $item_name,
"price" => $price,
"item_description" => $item_description
)
)
),
); //notice the variables
This is only just a sketching of the idea of what I want to achieve. But I couldn't wrap my head around this to make it work.
Any idea how to populate the first array according to the second array's value?
Related
I have an associative array key/values indicating a group. I'd like to shuffle the array so that the groups are in a random order but so that the items in the groups are shuffled only within their group. In other words, I want to take something like this:
[
[
"name" => "Buffy",
"group" => 1
],
[
"name" => "Willow",
"group" => 1
],
[
"name" => "Xander",
"group" => 2
],
[
"name" => "Giles",
"group" => 2
],
[
"name" => "Tara",
"group" => 3
],
[
"name" => "Angel",
"group" => 3
],
[
"name" => "Spike",
"group" => 3
]
]
And return something a bit more like this:
[
[
"name" => "Spike",
"group" => 3
]
[
"name" => "Angel",
"group" => 3
],
[
"name" => "Tara",
"group" => 3
],
[
"name" => "Willow",
"group" => 1
],
[
"name" => "Buffy",
"group" => 1
],
[
"name" => "Xander",
"group" => 2
],
[
"name" => "Giles",
"group" => 2
],
]
I realise this is probably possible with two or three sorts, but it would be great if this could be done with a single usort.
Basically, my solution shuffles the input array, temporarily restructures the input array to gather subarrays with the same group value, then returns the data to its original structure.
Code: (Demo)
shuffle($data); // randomize all subarrays
foreach ($data as $set) {
$grouped[$set['group']][] = $set; // merge subarrays on group value
}
$output = [];
foreach($grouped as $group) {
array_push($output, ...$group); // return to original array structure
}
var_export($output);
*Note that ... (splat operator) allows array_push() to store all subarrays (within each group) individually to generate the original structure.
Possible Output:
array (
0 =>
array (
'name' => 'Xander',
'group' => 2,
),
1 =>
array (
'name' => 'Giles',
'group' => 2,
),
2 =>
array (
'name' => 'Willow',
'group' => 1,
),
3 =>
array (
'name' => 'Buffy',
'group' => 1,
),
4 =>
array (
'name' => 'Spike',
'group' => 3,
),
5 =>
array (
'name' => 'Tara',
'group' => 3,
),
6 =>
array (
'name' => 'Angel',
'group' => 3,
),
)
Try...
$groups = [];
foreach (array_unique(array_column($myar, 'group')) as $k) $groups[$k] = rand();
foreach (array_keys($myar) as $k) $myar[$k]['rnd'] = rand();
usort($myar, function($a, $b) use ($groups) {
if ($gdif = $groups[$b['group']] - $groups[$a['group']]) return $gdif;
return $b['rnd'] - $a['rnd'];
});
foreach (array_keys($myar) as $k) unset($myar[$k]['rnd']);
print_r($myar);
Run with your data, this is one result...
Array
(
[0] => Array
(
[name] => Buffy
[group] => 1
)
[1] => Array
(
[name] => Willow
[group] => 1
)
[2] => Array
(
[name] => Angel
[group] => 3
)
[3] => Array
(
[name] => Tara
[group] => 3
)
[4] => Array
(
[name] => Spike
[group] => 3
)
[5] => Array
(
[name] => Giles
[group] => 2
)
[6] => Array
(
[name] => Xander
[group] => 2
)
)
I am having a hard time figuring out how to modify my array. The original array is this...
$array = array(
array(
"fruit" => "pineapple",
"id" => "aaa",
),
array(
"fruit" => "orange",
"id" => "aaa",
),
array(
"fruit" => "apple",
"id" => "bbb",
),
array(
"fruit" => "pear",
"id" => "bbb",
),
array(
"fruit" => "peach",
"id" => "ccc",
),
array(
"fruit" => "melon",
"id" => "ccc",
)
);
I need to convert this array into an array of keys by id. For example I would want the end result to look like this...
$array = array(
"aaa" => array("pineapple", "orange"),
"bbb" => array("apple", "pear"),
"ccc" => array("peach", "melon"),
);
I have attempted to loops through the array and pull out the things I need and rebuild the array but I cant figure it out. Hopefully someone can help me. Thanks!
You can do it by making a new array this way:
$array = array(
array(
"fruit" => "pineapple",
"id" => "aaa",
),
array(
"fruit" => "orange",
"id" => "aaa",
),
array(
"fruit" => "apple",
"id" => "bbb",
),
array(
"fruit" => "pear",
"id" => "bbb",
),
array(
"fruit" => "peach",
"id" => "ccc",
),
array(
"fruit" => "melon",
"id" => "ccc",
)
);
// empty array
$result = [];
// loop trough data
foreach($array as $value){
// add item to the $result array. Then make the id the key and the fruit the value
$result[$value['id']][] = $value['fruit'];
}
//print array
print_r($result);
The result will be
Array ( [aaa] => Array ( [0] => pineapple [1] => orange ) [bbb] => Array ( [0] => apple [1] => pear ) [ccc] => Array ( [0] => peach [1] => melon ) )
I want to merge two array's using key(product_id) and adding that values(usage).
Array 1
Array
(
[0] => Array
(
[name] => Reschedule A Service
[usage] => 1
[product_id] => 8
)
[1] => Array
(
[name] => Adding An Image
[usage] => 1
[product_id] => 5
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 1
[product_id] => 14
)
)
Array 2
Array
(
[0] => Array
(
[name] => Adding An Image
[usage] => 1
[product_id] => 5
)
[1] => Array
(
[name] => Schedule A Service
[usage] => 3
[product_id] => 11
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 2
[product_id] => 14
)
[3] => Array
(
[name] => Sales Performance Dashboard
[usage] => 2
[product_id] => 30
)
[4] => Array
(
[name] => Quote
[usage] => 1
[product_id] => 32
)
)
I need an out put like this merging and adding usage values.
Array
(
[0] => Array
(
[name] => Adding An Image
[usage] => 2
[product_id] => 5
)
[1] => Array
(
[name] => Schedule A Service
[usage] => 3
[product_id] => 11
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 3
[product_id] => 14
)
[3] => Array
(
[name] => Sales Performance Dashboard
[usage] => 2
[product_id] => 30
)
[4] => Array
(
[name] => Quote
[usage] => 1
[product_id] => 32
)
[5] => Array
(
[name] => Reschedule A Service
[usage] => 1
[product_id] => 8
)
)
This is my code for creating arrays
foreach($query->rows as $product){
$top_products[]=array(
'name'=>$product['name'],
'usage'=>$product['pusage'],
'product_id'=>$product['product_id']
);
}
foreach($query_2->rows as $product){
$top_point_products[]=array(
'name'=>$product['name'],
'usage'=>$product['p_usage'],
'product_id'=>$product['product_id']
);
}
$first =array(
array(
"name" => "Reschedule A Service",
"usage" => 1,
"product_id" => 8
),
array(
"name" => "Adding An Image",
"usage" => 1,
"product_id" => 5
),
array(
"name" => "Each Calendar Event",
"usage" => 1,
"product_id" => 14
)
);
$second =array(
array(
"name" => "Adding An Image",
"usage" => 1,
"product_id" => 5
),
array(
"name" => "Schedule A Service",
"usage" => 3,
"product_id" => 11
),
array(
"name" => "Each Calendar Event",
"usage" => 2,
"product_id" => 14
),
array(
"name" => "Sales Performance Dashboard",
"usage" => 2,
"product_id" => 30
),
array(
"name" => "Quote",
"usage" => 1,
"product_id" => 32
)
);
$result = array_unique(array_merge($first,$second), SORT_REGULAR);
Use array_unique & array_merge
Use the array_merge function, like this:
$C = array_merge($A, $B);
print_r($C);
Read manual Array merge
try this code
<?php
$array1=array
(
0 => array(
'name' => "Reschedule A Service",
'usage' => 1,
'product_id' => 8
),
1 => Array
(
'name' => "Adding An Image",
'usage' => 1,
'product_id' => 5
),
2 => Array
(
'name' => "Each Calendar Event",
'usage' => 2,
'product_id' => 14
)
);
$array2=array
(
0 => Array
(
'name' => "Adding An Image",
'usage' => 1,
'product_id' => 5
),
1 => Array
(
'name' => "Schedule A Service",
'usage' => 3,
'product_id' => 11
),
2 => Array
(
'name' => "Each Calendar Event",
'usage' => 5,
'product_id' => 14
),
3 => Array
(
'name' => "Sales Performance Dashboard",
'usage' => 2,
'product_id' => 30
),
4 => Array
(
'name' => "Quote",
'usage' => 1,
'product_id' => 32
)
);
$product_id1=array_column($array1, 'product_id');
$product_id2=array_column($array2, 'product_id');
$new=array_intersect($product_id1,$product_id2);
foreach ($new as $key => $value) {
if(in_array($new[$key],$product_id2)){
$array2[array_search($new[$key],$product_id2)]['usage']+=$array1[$key]['usage'];
}
}
$new1=array_diff($product_id1,$product_id2);
foreach ($new1 as $key => $value) {
$array2[]=$array1[$key];
}
foreach ($array2 as $key => $value) {
echo "[".$key."]=><br>";
foreach ($value as $key1 => $value1) {
echo "      ";
echo "[".$key1."]=>".$value1."<br>";
}
echo "<br>";
}
?>
output
[0]=>
[name]=>Adding An Image
[usage]=>2
[product_id]=>5
[1]=>
[name]=>Schedule A Service
[usage]=>3
[product_id]=>11
[2]=>
[name]=>Each Calendar Event
[usage]=>7
[product_id]=>14
[3]=>
[name]=>Sales Performance Dashboard
[usage]=>2
[product_id]=>30
[4]=>
[name]=>Quote
[usage]=>1
[product_id]=>32
[5]=>
[name]=>Reschedule A Service
[usage]=>1
[product_id]=>8
Use array_merge and a simple foreach loop to check your condition and update the usagevalues.
See below
$result = array_merge($arrArray1, $arrArray2);
$result2 = array();
foreach($result as $key => $value){
if(array_key_exists($value['product_id'], $result2)){
$result2[$value['product_id']]['usage'] += $value['usage'];
} else{
$result2[$value['product_id']] = $value;
}
}
print_r($result2);
If you want to reset your resultant array indexes use array_merge again like this
$result2 = array_merge($result2);
Hope this will help
i am facing problem in storing work and education information of Facebook user.
this is how i am getting info of user:
$profile_request=$fb->get('/me?fields=name,first_name,last_name,email,work,education');
and then convert the response in to multidimensional array:
$profile = $profile_request->getGraphNode()->asArray();
this is how i get the response:
$profile=Array
(
"name" => "abc pqr",
"first_name" => "abc",
"last_name" => "pqr",
"email" => "abc#gmail.com",
"work" => Array
(
"0" => Array
(
"employer" => Array
(
"id" => "348468651959125",
"name" => "Karachi university"
),
"location" => Array
(
"id" => "110713778953693",
"name" => "Karachi, Pakistan"
),
"position" => Array
(
"id" => "1556082398000870",
"name" => "Lecturer"
),
"start_date" => "2008-04-25"
),
"1" => Array
(
"description" => "i am teaching......",
"end_date" => "2011-06-20",
"employer" => Array
(
"id" => "486743441453589",
"name" => "Happy Palace Grammar School NORTH 3 - J2"
),
"location" => Array
(
"id" => "110713778953693",
"name" => "Karachi, Pakistan"
),
"position" => Array
(
"id" => "1556082398000870",
"name" => "Lecturer"
),
"start_date" => "2005-06-17"
)
),
"education" => Array
(
"0" => Array
(
"school" => Array
(
"id" => "193563427344550",
"name" => "Mehran Model Sec School"
),
"type" => "High School"
),
"1" => Array
(
"concentration" => Array
(
"0" => Array
(
"id" => "104076956295773",
"name" => "Computer Science"
)
),
"school" => Array
(
"id" => "113340788773685",
"name" => "Sir Adamjee Institute"
),
"type" => "College",
"year" => Array
(
"id" => "118118634930920",
"name" => "2012"
)
),
"2" => Array
(
"concentration" => Array
(
"0" => Array
(
"id" => "104076956295773",
"name" => "Computer Science"
)
),
"degree" => Array
(
"id" => "519451718218292",
"name" => "Bachelor of science in Computer Science in Computer Science"
),
"school" => Array
(
"id" => "107345492681211",
"name" => "SZABIST"
),
"type" => "Graduate School",
"year" => Array
(
"id" => "127342053975510",
"name" => "2016"
)
)
),
"id" => "145864899647476"
);
and this is my code:
$keys = array_keys($profile);
print_r($keys);
echo count($profile);
echo "<br>";
for ($i=4; $i <count($profile)-1 ; $i++) {
# code...
/*[4] => work [5] => education*/
echo $keys[$i]."<br>";
foreach ($profile[$keys[$i]] as $k => $v) {
# code...
echo "<br>$k<br>";
$keeys=array_keys($v);
print_r($keeys);
}
}
How can I loop through a multi-layer array and replace some associate values in it?
For instance, this is my array,
$items = array(
0 => array(
"id" => "1",
"title" => "parent 1",
"children" => array()
),
1 => array(
"id" => "2",
"title" => "parent 2",
"children" => array (
0 => array(
"id" => "4",
"title" => "children 1",
"granchildren" => array(
0 => array(
"id" => "7",
"title" => "granchildren 1"
),
1 => array(
"id" => "8",
"title" => "granchildren 2"
)
)
),
1 => array(
"id" => "5",
"title" => "children 2",
"granchildren" => array()
)
),
),
3 => array(
"id" => "3",
"title" => "parent 3",
"children" => array()
)
);
These are two working functions I have,
function translate ($id){
$items = array(
0 => array(
"id" => 1,
"title" => "parent 1 en"
),
1 => array(
"id" => 4,
"title" => "children 1 en"
),
2 => array(
"id" => 8,
"title" => "granchildren 2 en"
)
);
foreach($items as $item) {
if($id === $item['id'])
{
return $item['title'];
}
}
}
function looper ($items){
$new_items = array();
foreach($items as $key => $item) {
if(isset($key) && is_array($key)){
$new_items[$key] = translate($item['id']);
}else {
//looper($item);
}
}
return $new_items;
}
print_r(looper ($items));
This is the result I am after,
Array
(
[0] => Array
(
[id] => 1
[title] => parent 1 en // translated
[children] => Array
(
)
)
[1] => Array
(
[id] => 2
[title] => parent 2
[children] => Array
(
[0] => Array
(
[id] => 4
[title] => children 1 en // translated
[granchildren] => Array
(
[0] => Array
(
[id] => 7
[title] => granchildren 1
)
[1] => Array
(
[id] => 8
[title] => granchildren 2 en // translated
)
)
)
[1] => Array
(
[id] => 5
[title] => children 2
[granchildren] => Array
(
)
)
)
)
[3] => Array
(
[id] => 3
[title] => parent 3
[children] => Array
(
)
)
)
Is it possible?
Sounds like a job for array_walk or array_walk_recursive.
It will call a user-supplied function for every item in an array. You can have it modify the array by reference to achieve what you're after.