php multidimensional array, implode twice? - php

I need some help trying to implode my multidimensional array twice. I'm using Joomla 2.5 and it's for a backend component..
Here's what the array is:
Array
(
[jform] => Array
(
[options] => Array
(
[colour] => Array
(
[0] => a
[1] => d
)
[size] => Array
(
[0] => b
[1] => e
)
[qty] => Array
(
[0] => c
[1] => f
)
)
)
)
I've tried using the following code:
$i=0;
$optchildArr = array();
$optchildArrX = array();
foreach ($this->options as $optArr) :
$j=0;
foreach ($optArr as $arr) :
$optchildArrX[] = $arr;
$j++;
endforeach;
$optchildArr[$i] = implode(',',$optchildArrX);
$i++;
endforeach;
$this->options = implode(';',$optchildArr);
But I'm getting these kind of results:
[options] => Array
(
[0] => a,d
[1] => a,d,b,e
[2] => a,d,b,e,c,f
)
When I'm after:
[options] => Array
(
[0] => a,b,c
[1] => d,e,f
)
Any help would be greatly appreciated!! :)

Assuming the main array is $A
function mergeArrays($colour, $size, $qty) {
$result = array();
for ($i=0; $i<count($colour); $i++) {
//Assuming three arrays have the same length;
$result[] = implode(',',array($colour[$i], $size[$i], $qty[$i]));
}
return $result;
}
$result_array = array_map(
'mergeArrays',
$A['jform']['options']['colour'],
$A['jform']['options']['size'],
$A['jform']['options']['qty']
);
//Check the output from this, should be the output you described.
var_dump($result_array);

Related

Change value inside an array

I'd like to change the values of an array.
Currently my array looks like this:
Array
(
[0] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
)
)
I want to remove everything before the + symbol, so in the end the new array will looke like this
Array
(
[0] => Array
(
[0] => Yes
[1] => N/A
[2] => No
)
)
This is my code:
$new_array = array();
foreach( $array as $key => $value ) {
$split = explode("+", $value[0]);
$new_array[] = $split[1];
}
Hoping that it would worked, but when I check the new array, it only shows one value.
Array
(
[0] => Yes
)
Any help in putting me in the right direction is much appreciated.
Please, check it:
<?php
$array[0][0] = '12-Multi_select-customfield-retina-ready+Yes';
$array[0][1] = '12-Multi_select-customfield-retina-ready+N/A';
$array[0][2] = '112-Multi_select-customfield-retina-ready+No';
echo '<pre>';
print_r($array);
$new_array = array();
foreach( $array[0] as $key => $value ) {
$split = explode("+", $value);
$new_array[] = $split[1];
}
print_r($new_array);
echo '</pre>';
Try This this work even if you have multiple key in the original array $original_array[0], $original_array[1] ... :
$original_array[0] = [
0 => '12-Multi_select-customfield-retina-ready+Yes',
1 => '12-Multi_select-customfield-retina-ready+N/A',
2 => '12-Multi_select-customfield-retina-ready+No'
];
print_r($original_array);
$new_array = [];
foreach ($original_array as $key => $value) {
foreach ($value as $index => $val) {
$split = explode("+", $val);
$new_array[$key][] = $split[1];
}
}
print_r($new_array);
Example :
Original array
Array
(
[0] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
),
[1] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
)
)
New Array
Array
(
[0] => Array
(
[0] => Yes
[1] => N/A
[2] => No
),
[1] => Array
(
[0] => Yes
[1] => N/A
[2] => No
)
)

php array unique values using array_unique

Array
(
[0] => Array
(
[0] => abc#test.com
[1] => qwrt#sometest.com
[2] => haritha#elitesin.com
)
[1] => Array
(
[0] => Kanishka.Kumarasiri#elitesin.com
[1] => Haritha#elitesin.com
)
[2] => Array
(
[0] => Kanishka.Kumarasiri#elitesin.com
[1] => test#elitesin.com
)
)
I have an array like this and I want to get unique values from this array.
But my code is failing
for ($i = 0; $i < count($return_arr); $i++) {
$new_value[] = explode(",", $return_arr[$i]);
}
print_r (array_unique($new_value));
It says array to string conversion error
i want the array to be like this get only the unique email ids
Array
(
[0] => Array
(
[0] => abc#test.com
[1] => qwrt#sometest.com
[2] => haritha#elitesin.com
[3] => Kanishka.Kumarasiri#elitesin.com
[4] => test#elitesin.com
)
)
Because your code is wrong, you are trying to explode something which is not contained in your array, try this code:
<?php
$arr = array("0"=>array("abc#test.com","qwrt#sometest.com","haritha#elitesin.com"),
"1"=>array("Kanishka.Kumarasiri#elitesin.com,Haritha#elitesin.com"),
"2"=>array("Kanishka.Kumarasiri#elitesin.com,test#elitesin.com"));
$allEmails = array();
foreach($arr as $array){
foreach($array as $email){
$allEmails[] = explode(",",$email);
}
}
$new_value = array();
foreach($allEmails as $array){
foreach($array as $email){
$new_value[] = strtolower($email);
}
}
print_r (array_unique($new_value));
?>

PHP How to restructure an array?

I have an array that I'd like to restructure. I want to group items by turn. I can figure out how to extract data from the array using foreach($arr['history'] as $obj) my issue is with populating a new array using a loop.
Currently it looks like this:
Array (
[history] => Array (
[id] => 23452435
[legend] => Array (
[0] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => foo
)
)
[1] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => bar
)
)
[2] => Array (
[player] => opponent
[turn] => 1
[card] => Array (
[name] => derp
)
)
[3] => Array (
[player] => opponent
[turn] => 2
[card] => Array (
[name] => hoo
)
)
)
))
I want it to look like the following, but I can't figure out how to automatically create and populate this structure. This is an array with a sub-array for each turn, containing an array for me and opponent
Array (
[0] => Array (
[me] => Array (
[0] => foo
[1] => bar
)
[opponent] = Array (
[0] => derp
)
)
[1] => Array (
[me] => Array ()
[opponent] => Array (
[0] => hoo
)
))
Thanks.
Edit:
This is what I needed. Thanks for the answers.
$result = [];
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $list) {
$result[$list['turn']][$list['player']][] = $list['card']['name'];
}
}
Try this:
$result = [];
foreach ($data['history']['legend'] as $list) {
$result[$list['turn']-1][$list['player']][] = $list['card']['name'];
}
Fiddle it! http://ideone.com/BtKOKJ
You can just start adding data to the new array. PHP is extremely forgiving.
$historyByTurns = array();
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $legendItem) {
$turn = $legendItem['turn'];
$player = $legendItem['player'];
if (!array_key_exists($turn, $historyByTurns)) {
$historyByTurns[$turn] = array();
}
if (!array_key_exists($player, $historyByTurns[$turn])) {
$historyByTurns[$turn][$player] = array();
}
foreach ($legendItem as $card) {
$historyByTurns[$turn][$player][] = $card['name'];
}
}
}
You will have to test it, as I have no way to do that ATM.

add values in php based off first value php

I have an array as shown below which some rows will have the same name. I am getting nowhere fast trying to do what i want to achieve.
With a foreach loop how i would go through each one add the values of the same name up and put it into a new array?
Thanks :)
Array
(
[0] => Array
(
[0] => Name1
[1] => Value
)
[1] => Array
(
[0] => Name2
[1] => Value
)
[2] => Array
(
[0] => Name1
[1] => Value
)
[3] => Array
(
[0] => Name3
[1] => Value
)
[4] => Array
(
[0] => Name2
[1] => Value
)
This only a snippet and will change to length when the csv it comes from changes weekly Thanks
It's not the most efficient way yet but it's working the way you want it to happen.
<?php
$testArr = array(
array('Name1', 1),
array('Name2',2),
array('Name1',3)
);
$newArr = array();
$tmp = array();
foreach($testArr as $i=>$row)
{
if( in_array($row[0], $tmp) )
{
$key = array_search($row[0], $tmp);
$newArr[ $key ][1] += $row[1];
}
else
{
array_push( $newArr, array($row[0], $row[1]) );
array_push( $tmp, $row[0] );
}
}
print_r($newArr);
?>
OUTPUT
Array
(
[0] => Array
(
[0] => Name1
[1] => 4
)
[1] => Array
(
[0] => Name2
[1] => 2
)
)
Tested working, Check it here, phpFiddle
Do you want something like this?
$newData = [];
foreach ($array as $item) {
$newData[$item[0]][] = $item[1];
}
var_dump($newData);
You can get to the result with this approach.
$data_array = Array{};
$result_array = NULL;
foreach ($data_array as $value) {
$result_array[$value[0]] += $value[1];
}

Array values update

I have an array like this..
Array
(
[0] => Array
(
[0] => 1``
[1] => 2``
[2] => 3``
)
[1] => Array
(
[0] => 4``
[1] => 5``
[2] => 6``
)
[2] => Array
(
[0] =>
[1] => 7``
[2] =>
)
)
I want the result like this below,
$remaining_value = Array
(
[0] => 1`` 4``,
[1] => 2`` 5`` 7``,
[2] => 3`` 6``,
)
How to do this in an single loop.. Plz help me..
If the lower-level arrays will always have the same number of elements then you can do something like this:
$subArrayCount = count( $inputArray );
$outputArray = array();
$firstSubArray = reset( $inputArray );
foreach( $firstSubArray as $key => $value )
{
$outputArray[$key] = $value;
for( $innerLoop = 1; $innerLoop < $subArrayCount; $innerLoop++ )
{
$outputArray[$key].= $inputArray[$innerLoop][$key];
}
}
var_dump( $outputArray );
This should work:
<?php
$remaining_value=array();
foreach($array as $loopv1){
foreach($loopv1 as $key2 => $loopv2){
if(empty($remaining_value[$key2]))$remaining_value[$key2]=$loopv2; else $remaining_value[$key2].=" ".$loopv2;
}
}
?>

Categories