I have some array like this
Array(
[0] => {"puroriid":"3902598","purorid":"3901727","iid":"3927478"}
[1] => {"puroriid":"3902599","purorid":"3901727","iid":"3927486"}
[2] => {"puroriid":"3902600","purorid":"3901727","iid":"3927486"}
)
Here i want to add some data to at the end of each looping so data will look as follow.
Array(
[0] => {"puroriid":"3902598","purorid":"3901727","iid":"3927478","variation_name"=>"tiles" ,"hsn" =>"42424"}
[1] => {"puroriid":"3902599","purorid":"3901727","iid":"3927486","variation_name"=>"wood","hsn" =>"63636"}
[2] => {"puroriid":"3902600","purorid":"3901727","iid":"3927486","variation_name"=>"granite","hsn" =>"66656"}
)
I tried array push method but its created another index , instead of adding new data to last
below is my code.
$items = array();
$variations = array();
if ($purchaseOrderDetails->getOrderItems())
{
foreach ($purchaseOrderDetails->getOrderItems() as $key => $item)
{
$items[] = strval($item);
$variations[] = strval(new InventorySetVariation($item->getIsvid()));
}
}
Utility::ajaxResponseTrue("", array("po" => strval($purchaseOrderDetails), "items" => $items, "variations" => $variations));
Here I want to merge item and variations as one array .
how can I achieve it ?
Below is the example may help you to achieve similar goals of yours
<?php
$items = array(
0 => '{"puroriid":"3902598","purorid":"3901727","iid":"3927478"}',
1 => '{"puroriid":"3902599","purorid":"3901727","iid":"3927486"}',
2 => '{"puroriid":"3902600","purorid":"3901727","iid":"3927486"}'
); //Your Items
$itmesNew = [];
foreach($items as $val)
{
$newItem = json_decode($val,true);
$newItem['variation_name'] = 'test'; //New Items Append
$newItem['hsn'] = '123'; //New Items Append
$itmesNew[] = json_encode($newItem); //Add as json
}
echo "<pre>";
print_r($itmesNew);
?>
Output
Array
(
[0] => {"puroriid":"3902598","purorid":"3901727","iid":"3927478","variation_name":"test","hsn":"123"}
[1] => {"puroriid":"3902599","purorid":"3901727","iid":"3927486","variation_name":"test","hsn":"123"}
[2] => {"puroriid":"3902600","purorid":"3901727","iid":"3927486","variation_name":"test","hsn":"123"}
)
Related
I got an array of links which I am getting from source code. I am looping through the array with a foreach loop and adding the results into a new array.
The problem is: I don't want all the results in one array. But for each link a separate array after I looped over it.
The array I am looping through:
Array
(
[0] => Array
(
[0] => http://videos.volkswagen.nl/videos/videos/
)
[1] => Array
(
[0] => http://videos.volkswagen.nl/videos/service-videos/
)
)
The foreach:
$sourceCats = array();
foreach ($matchesAll as $links) {
$strSourceAll = implode("|",$links);
$source = file_get_contents("$strSourceAll");
htmlspecialchars($source);
$sourceCats[] = $source;
}
How the array sourceCats looks now:
Array
(
[0] => (source code from first link)
[1] => (source code from second link)
)
How I want it to look like:
Array
(
[0] => Array
(
[0] => (source code from first link)
)
[1] => Array
(
[0] => (source code from second link)
)
)
I have tried a few things but nothing worked. Is the idea clear?
Any help will be much appreciated.
<?php
$finalsourceCats = array();
$counter_sourceCats = 0;
$matchesAll = array(
0 => array(
0 => "http://videos.volkswagen.nl/videos/videos/"
),
1 => array(
0 => "http://videos.volkswagen.nl/videos/service-videos/"
)
);
foreach ($matchesAll as $links) {
$sourceCats = 'sourceCats';
$sourceCats = $sourceCats . "_" . $counter_sourceCats;
$sourceCats = array();
$strSourceAll = implode("|", $links);
$source = file_get_contents("$strSourceAll");
htmlspecialchars($source);
$sourceCats[] = $source;
$finalsourceCats[] = $sourceCats;
$counter_sourceCats += 1;
}
echo "<pre>"; print_r($finalsourceCats);
I have following foreach loop :
foreach($mydata as $data){
$section = $data->section;
$category = $data->category;
$item = $data->item;
}
I want to build tree like this :
- section
_ category
- items
...
For now i did like this :
$tmp = [];
$newarray = [];
foreach($mydata as $data){
$section = $data->section;
$category = $data->category;
$item = $data->item;
if( !in_array($category, $temp)){
$newarray[$section][] = $category;
}
}
// output:
Array(
[SectionName_one] => Array(
[0] => categoryOneName_one
[1] => categoryOneName_two
[2] => categoryOneName_three
)
[SectionName_two] => Array(
[0] => categoryTwoName_one
[1] => categoryTwoName_two
[2] => categoryTwoName_three
)
...
)
And i am blocked here, i don't know how to insert items elements for each category, if you hava an idea thanks to help me :)
Thank you
foreach ($mydata as $data){
$newarray[$data->section][$data->category][]= $data->item;
}
PHP is quite forgiving with non-existent array keys.
I'm trying to store data in an array from a database.
After printing the array, I need something like this :
Array ( [ABBA] => ?search=ABBA [ACDC] => ?search=ACDC [Ace of Spades] => ?search=AceOfSpades)
But currently, I have this :
Array ( [url] => ?search=ABBA [title] => ABBA ) Array ( [idtitle] => ?search=ACDC [title] => ACDC ) Array ( [idtitle] => ?search=AceOfSpades [title] => Ace of Spades )
Here is my code to get the data and store into the array :
$key = $_POST['latestQuery'];
$requete = $bdd->prepare('SELECT url, title FROM articles WHERE title LIKE :key LIMIT 10');
$requete->execute(array('key' => '%'.$key.'%'));
$result_array[] = array();
foreach($requete->fetchAll(PDO::FETCH_ASSOC) as $search)
{
print_r($result_array[$search['title']] = $search);
}
And here this my table structure :
url | title
$search=ACDC | ACDC
Do you think there is a way to formate my array?
Remove print_r from a foreach:
$result_array = array();
foreach($requete->fetchAll(PDO::FETCH_ASSOC) as $search)
{
$result_array[$search['title']] = $search['url'];
}
print_r($result_array);
You can try this:
// testing array
$yourArray = array(
array(
'url'=>'?search=ABBA',
'title'=>'ABBA',
),
array(
'url'=>'?search=BABA',
'title'=>'BABA',
),
array(
'url'=>'?search=CBAA',
'title'=>'CBAA',
),
);
// solution
$myArr = array();
foreach ($yourArray as $key => $value) {
$myArr[$value['title']] = $value['url'];
}
echo "<pre>";
print_r($myArr);
Result is:
Array
(
[ABBA] => ?search=ABBA
[BABA] => ?search=BABA
[CBAA] => ?search=CBAA
)
I want to convert multi dimensional array in Php
I'm stuck in logic.. Kindly help
Thanks in advance
Current Produced array :
Array
(
[5316] => Array
(
[0] => Array
(
[PROD1] => color=black
)
[1] => Array
(
[PROD1] => paper=a1
)
[2] => Array
(
[PROD2] => color=metallic_silver
)
[3] => Array
(
[PROD2] => paper=a1
)
)
)
I want to convert this array into this form
Array
(
[5316] => Array
(
[PROD1] => Array
(
color => black
paper => a1
)
[PROD2] => Array
(
color => metallic_silver
paper => a1
)
)
)
If you can't hardcode that key to directly point to it, you can use reset() in this case, then grouped them inside a foreach accordingly. Example:
$array = array(
5316 => array(
array('PROD1' => 'color=black'),
array('PROD1' => 'paper=a1'),
array('PROD2' => 'color=metallic_silver'),
array('PROD2' => 'paper=a1'),
),
);
$grouped = array();
// point it to the first key which gives an array of those values
foreach (reset($array) as $key => $value) {
reset($value); // reset the internal pointer of the sub array
$key = key($value); // this return `PROD1, PROD2`
$grouped[$key][] = current($value); // current gives color=black, the values
}
$array[key($array)] = $grouped; // then reassign
echo '<pre>';
print_r($array);
Sample Output
You can try something like below.
$newArr = [];
$count = count($arr[5316]);
for($i = 0, $j = 1; $i < $count; $i += 2){
$color = explode('=', $arr[5316][$i]['PROD'.$j]);
$paper = explode('=', $arr[5316][$i+1]['PROD'.$j]);
$newArr[5316]['PROD'.$j] = array('color'=>$color[1], 'paper'=>$paper[1]);
$j++;
}
//To show output
print '<pre>';
print_r($newArr);
print '</pre>';
I am struggling with an array that I need to convert into a new array based on the unique array keys. My current result looks like below. Each array represent just a part of the desired result (pivot) where I need a [menu_name], [menu_url] and [menu_target] as result and when the next array begins with the same keys, etc. So the way I see it to achieve this is to construct a new array, each time an array_key_exist in the array. But i am unable to achieve this.
Array
(
Array
(
[menu_name] => Contact
)
Array
(
[menu_url] => /contact
)
Array
(
[menu_target] => _blank
)
Array
(
[menu_name] => Home
)
Array
(
[menu_url] => /home
)
Array
(
[menu_target] => _self
)
)
The desired array I want to create looks like this:
Array
(
[0] => Array
(
[menu_name] => Contact,
[menu_url] => /contact,
[menu_target] => _blank
)
[1] => Array
(
[menu_name] => Home,
[menu_url] => /home,
[menu_target] => _blank
)
)
Here is my code so far (incomplete):
$result = array();
foreach($array as $option => $value)
{
$result[$value->option_key] = $value->option_value;
$new_array = array();
if(array_key_exist($value->option_key, $new_array))
{
// here is where I get stuckā¦.
print_r($new_array);
}
}
I hope some one can get me in the right direction to further complete the code with the desired result.
You can use a var you increment each time key already exists :
$result = array();
$i = 0;
foreach($array as $option => $value)
{
if ( array_key_exists($value->option_key, $result[$i]) ) $i++;
$result[$i][$value->option_key] = $value->option_value;
}
Another way, is if the current batch of keys are complete, go to the next one and fill up the next one. Example:
$values = array(array('menu_name' => 'Contact'),array('menu_url' => '/contact'),array('menu_target' => '_blank'),array('menu_name' => 'Home'),array('menu_url' => '/home'),array('menu_target' => '_self'),);
$new_values = array();
$x = 0;
$columns = array_unique(array_map(function($var){
return key($var);
}, $values));
foreach($values as $value) {
$current_key = key($value);
$new_values[$x][$current_key] = reset($value);
if(array_keys($new_values[$x]) == $columns) $x++;
}
echo '<pre>';
print_r($new_values);
Sample Demo
Provided your answer is always grouped by threes, as posted in your example.
This is an alternative method to djidi's answer incase you wanted to see it done with silly loops.
$new = array_chunk($a, 3);
$d = array();
foreach($new as $i => $group) {
foreach($group as $index => $item) {
foreach($item as $name=>$val) {
$d[$i][$name] = $val;
}
}
}
Which returns:
Array
(
[0] => Array
(
[menu_name] => Contact
[menu_url] => /contact
[menu_target] => _blank
)
[1] => Array
(
[menu_name] => Home
[menu_url] => /home
[menu_target] => _self
)
)
Example Demo