I have total 3 array
First array contains list of titles
Second array contains list of description
third array contains images
I have already combine 1st and 2nd array as a key of first array and value of second array and I also combine 1st and 3rd array as key of 1st and value of 3rd.
Following is my arrays
1 )
Array
(
[First] => FFFFFFFFFFFFFFFF
[Second] => ssss
[0] => Array
(
[First] => eae2d7b3f20250def1892bae1abaf07f.png
[Second] => ea7ca514d1ef580f85fb42c7cb425462.png
)
)
I want output like
Array
(
[First] => FFFFFFFFFFFFFFFF
[Second] => ssss
[First] => eae2d7b3f20250def1892bae1abaf07f.png
[Second] => ea7ca514d1ef580f85fb42c7cb425462.png
)
Code
foreach ($images as $key => $value) {
$values['image']= $value;
}
$data = array_combine($_POST['title'], $images);
$mainArray = array_combine($_POST['title'], $_POST['Description']);
array_push($mainArray,$data);
echo '<pre>';
print_r($mainArray);
How can I do this?
The code below should produce the format the array as required above, although it is not the most elegant way to flatten an array. It may be worthwhile looking at previous questions like How to Flatten a Multidimensional Array?
$newArr = array();
foreach ( $mainArray as $key => $val ) {
if ( is_array( $val ) ) {
foreach ( $val as $key2 => $val2 ) {
$newArr[$key2] = $val2;
}
} else {
$newArr[$key] = $val;
}
}
Related
I have defined two arrays as
$array1 = (8,10);
Array2 was array of stdobjects which was later converted into below using json decode, encode. Php echo output of the same is below:
$array2 = Array
(
[0] => Array
(
[id] => 6
)
[1] => Array
(
[id] => 8
)
[2] => Array
(
[id] => 10
)
)
Later I created one array
foreach( $array2 as $value )
{
$valuesArray[] = array('',$value['id'],Input::get('date'),'0');
}
What I am trying to do is compare array1 with valuesarray. If $value['id'] i.e. second element matches with any of the element in array1, I will save 4th element of $nnn as 1. If it doesnt match with any of the element, I will save it as 0.
My code below:
foreach ($valuesArray as $value2)
{
foreach ($array1 as $value1)
{
if ($value2[1] == $value1)
{$x = 1;}
else
{$x = 0;}
}
$nnn[] = "('','".$value2[1]."','".Input::get('date')."','".$x."')";
}
echo '<pre>',print_r($nnn,1),'</pre>';
The output that I am getting is:
Array
(
[0] => ('','6','2016-04-25','0')
[1] => ('','8','2016-04-25','0')
[2] => ('','10','2016-04-25','1')
)
Correct output should be:
Array
(
[0] => ('','6','2016-04-25','1')
[1] => ('','8','2016-04-25','1')
[2] => ('','10','2016-04-25','0')
)
try this:
$nnn = array();
foreach ($valuesArray as $value) {
$x = (in_array($value[1], $array1))?1:0;
$nnn[] = "('','".$value[1]."','".Input::get('date')."','{$x}')";
}
I'm trying to get the key of a subarray based on a value in that subarray. So, based on the example below, how can I return the key of the array containing 'apple'?
Array (
[0] => Array (
[fruit] => apple
[colour] => green
)
[1] => Array (
[fruit] => banana
[colour] => yellow
)
)
So logically, something like:
if ('apple' is in $subarray) {
echo $subarray_key;
}
Thanks in advance.
Assuming that your array is stored in $arr variable, you can do
foreach($arr as $key => $value){
if(in_array('apple',$value){
echo $key;
}
}
foreach($array as $key => $val){
if($val == 'apple'){
print $key;
}
}
You can use array keys and do some other stuff but for the most part you're going to end up just iterating through the array anyways
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];
}
I have an 2d array which returns me this values:
Array (
[0] => Array (
[0] => wallet,pen
[1] => perfume,pen
)
[1] => Array (
[0] => perfume, charger
[1] => pen,book
).
Out of this i would like to know if it is possible to create a function which would combine the array going this way,and create a new one :
if for example [0] => Array ( [0] => wallet,pen [1] => perfume,pen ) then should be equal to
[0] => Array ( [0] => wallet,pen, perfume ) because there is a common word else do nothing.
And also after that retrieve each words as strings for further operations.
How can i make the values of such an array unique. Array ( [0] => Array ( [0] => wallet [1] => pen [2] => perfume [3] => pen) ) as there is pen twice i would like it to be deleted in this way ( [0] => Array ( [0] => wallet [1] => pen [2] => perfume) )
It's just a matter of mapping the array and combining the inner arrays:
$x = [['wallet,pen', 'perfume,pen'], ['perfume,charger', 'pen,book']];
$r = array_map(function($item) {
return array_unique(call_user_func_array('array_merge', array_map(function($subitem) {
return explode(',', $subitem);
}, $item)));
}, $x);
Demo
This first splits all the strings based on comma. They are then merged together with array_merge() and the duplicates are removed using array_unique().
See also: call_user_func_array(), array_map()
Try this :
$array = Array (Array ( "wallet,pen", "perfume,pen" ), Array ( "perfume, charger", "pen,book" ));
$res = array();
foreach($array as $key=>$val){
$temp = array();
foreach($val as $k=>$v){
foreach(explode(",",$v) as $vl){
$temp[] = $vl;
}
}
if(count(array_unique($temp)) < count($temp)){
$res[$key] = implode(",",array_unique($temp));
}
else{
$res[$key] = $val;
}
}
echo "<pre>";
print_r($res);
output :
Array
(
[0] => wallet,pen,perfume
[1] => Array
(
[0] => perfume, charger
[1] => pen,book
)
)
You can eliminate duplicate values while pushing them into your result array by assigning the tag as the key to the element -- PHP will not allow duplicate keys on the same level of an array, so any re-encountered tags will simply be overwritten.
You can use recursion or statically written loops for this task.
Code: (Demo)
$result = [];
foreach ($array as $row) {
foreach ($row as $tags) {
foreach (explode(',', $tags) as $tag) {
$result[$tag] = $tag;
}
}
}
var_export(array_values($result));
Code: (Demo)
$result = [];
array_walk_recursive(
$array,
function($v) use(&$result) {
foreach (explode(',', $v) as $tag) {
$result[$tag] = $tag;
}
}
);
var_export(array_values($result));
I've two PHP Arrays. The first one contains a sort order. The second one contains the data which i need to sort. I have no idea how to solve it…
What I'm trying to get is a list, sorted by the values of the first array (order.txt). Any suggestions?
<li>Item [2]</li>
<li>Item [1]</li>
<li>Item [3]</li>
Order
Array
(
[0] => 2
[1] => 1
[2] => 3
)
Data
Array
(
[0] => Array
(
[id] => 1
[name] => 00134258.jpg
[size] => 2787
)
[1] => Array
(
[id] => 2
[name] => 80132454.jpg
[size] => 2667
)
[2] => Array
(
[id] => 3
[name] => 13134218.jpg
[size] => 2787
)
)
Here are the code which produces the arrays above:
<?php
$order = file('order.txt');
foreach ($order as $key => $value) {
$order = json_decode($value, true);
}
print_r($order);
$file = file('db.txt');
foreach ($file as $key => $value) {
$file_data[] = json_decode($value, true);
}
print_r($file_data);
?>
This are the json strings:
order.text
{"0":"2","1":"1","2":"3"}
db.txt
{"id":"1","name":"00134258.jpg","size":2787}
{"id":"2","name":"80132454.jpg","size":2667}
{"id":"3","name":"13134218.jpg","size":2787}
Make a new array where the keys are the id of the data, then loop through your order array and assign the values to an ordered array...
<?php
// loop through the file data
foreach($file_data as $v){
// assign values to new array with the data id as it's key
$identified[$v['id']] = $v;
}
// loop through the order array
foreach($order as $v){
// pull the data values from the identified array by their key
$ordered[] = $identified[$v];
}
// check it has all worked out as planned ;)
print_r($ordered);
?>
Alternatively... in line with #hakre's method, first create array ordered by index, and then use array_multisort method.
<?php
foreach($file_data as $v){
$ordered[$v['id']] = $v;
}
array_multisort($order, $ordered);
print_r($ordered);
?>
From your $order array substract 1 from each value, then use array_multisort:
foreach($order as &$o) $o--;
unset($o);
array_multisort($order, $data);
This works as long as the id value is always one higher than it's offset in $data.