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);
Related
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"}
)
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
I have following array,
Array
(
[Char100_1] => Array
(
[0] => Array
(
[Char100_1] => Mr S Kumar
)
[1] => Array
(
[Char100_1] => Mr S Kumar2
)
)
[Char100_13] => Array
(
[0] => Array
(
[Char100_13] => 159.9
)
[1] => Array
(
[Char100_13] => 119.9
)
)
[Char100_14] => Array
(
[0] => Array
(
[Char100_14] => 191.88
)
[1] => Array
(
[Char100_14] => 143.88
)
)
)
which is created dynamically from a database query result and some loops.
Now I wanted to convert this array into something like below,
Array
(
[0] => Array
(
[Char100_1] => Mr S Kumar
[Char100_13] => 159.9
[Char100_14] => 191.88
)
[1] => Array
(
[Char100_1] => Mr S Kumar2
[Char100_13] => 119.9
[Char100_14] => 143.88
)
)
I have tried looping through them but its not working.
<?php
/* database process to create array */
$contentArray = array();
foreach($newData['DataField'] as $ndata) :
$responsedata = getAppContent($appid, $ndata);
while($haveresult = mysql_fetch_assoc($responsedata))
{
$contentArray[$ndata][] = $haveresult;
}
endforeach;
/* for getting resulting array start */
$newdataArray = array();
foreach($contentArray as $field => $value):
$newdataArray[$field] = array();
foreach( $value as $val ) :
$newdataArray[$field] = $val;
endforeach;
endforeach;
?>
If you can't change the query (as suggested in the comments), then the following should work:
$output = array();
foreach ($array as $a) {
foreach ($a as $k => $b) {
if (empty($output[$k])) {
$output[$k] = array();
}
$output[$k] += $b;
}
}
I observe that you are transposing the arrays. i.e all the zero subscript values together and all the one subscript values together.
Therefore your outer subscript should be the '0' and '1'. These are available in the inner loop. So, the inner loop index becomes the outer array index. And the inner loop value, which is an array, you need to take the 'current' value of.
/* for getting resulting array start (PHP 5.3.18) */
$newdataArray = array();
foreach($contentArray as $field => $value):
foreach( $value as $idx => $val ): // $idx takes value 0 or 1. $val is an array
$newdataArray[$idx][$field] = current($val);
endforeach;
endforeach;
print_r($newdataArray);
As long as all of your arrays have the same amount of values containing them a for loop will do:
$NewDataArray = array();
for ($a = 0; $a < $Numberofvaluesineacharray; $a++){
$NewDataArray[$a] = $NewDataArray[$array1[$a], $array2[$a],.....arrayn[$a];
}
Hello I have an array that consists of two other arrays within it using the following code:
foreach($relations as $rel){
$data[$i]["relationTo"] = $rel["name"];
$data[$i]["relation"] = $rel["relation"];
$i = $i+1;
}
foreach($relations as $rel){
$children[$i]["id"] = $rel["id2"];
$children[$i]["name"] = $rel["sname"];
$children[$i]["data"] = $data;
$i = $i+1;
}
foreach($relations as $rel){
$relationArray[$i]["id"] = $rel["id"];
$relationArray[$i]["name"] = $rel["name"];
$relationArray[$i]["children"] = $children;
$i = $i+1;
}
When I print this out using:
print_r($relationArray);
It prints the following:
Array ( [2] => Array ( [id] => 4 [name] => Albaraa [children] =>
Array ( [1] => Array ( [id] => 5 [name] => Sadi [data] =>
Array ( [0] => Array ( [relationTo] => Albaraa [relation] => Father ) ) ) ) ) )
I am using json_encode and I need it to be output in json a certain way not including the indexed count of arrays in the beginning...the json output when I use:
echo json_encode($relationArray);
is like this currently:
{"2":{"id":"4","name":"Albaraa","children":
{"1":{"id":"5","name":"Sadi","data": [{"relationTo":"Albaraa","relation":"Father"}]}}}}
With the "2" and "1" in front of what the first 2 arrays are...which is not what I am trying to achieve which would be like this:
{"id":"4","name":"Albaraa","children":
{"id":"5","name":"Sadi","data": [{"relationTo":"Albaraa","relation":"Father"}]}}}}
Any help will be much appreciated!
Several solutions
1) Do not enter values by [$i], prepare new complete inner array and put it inside with array_push
2) If you still want to do this way you can extract just the values:
print_r(json_encode(array_values($array)));