Array values update - php

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;
}
}
?>

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 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.

How to parse this simple text-block into a multi-dimensional array, in PHP?

I have a simple text-block; and, its content is:
txt_1(val_1,val_2,val_3).
txt_1(val_4,val_5,val_6).
txt_2(val_7,val_8,val_9).
txt_3(val_10,val_11,val_12).
txt_3(val_13,val_14,val_15).
txt_4(val_16,val_17,val_18).
And, there is already an simple array, in PHP code:
$my_array = array();
Now, I want to parse into this PHP array, like:
Array
(
[txt_1] => Array
(
[0] => Array
(
[0] => val_1
[1] => val_2
[2] => val_3
)
[1] => Array
(
[0] => val_4
[1] => val_5
[2] => val_6
)
)
[txt_2] => Array
(
[0] => Array
(
[0] => val_7
[1] => val_8
[2] => val_9
)
)
[txt_3] => Array
(
[0] => Array
(
[0] => val_10
[1] => val_11
[2] => val_12
)
[1] => Array
(
[0] => val_13
[1] => val_14
[2] => val_15
)
)
[txt_4] => Array
(
[0] => Array
(
[0] => val_16
[1] => val_17
[2] => val_18
)
)
)
All data is general. Could you help me to do it, with PHP?
<?php
// Initially put your input into a variable
$txt=<<<__EOT__
txt_1(val_1,val_2,val_3).
txt_2(val_4,val_5,val_6).
txt_n(val_a,val_b,val_c).
__EOT__;
$result = array();
// separate out each row
$rows = explode("\n", $txt);
// loop through each row
foreach($rows as $row) {
// Use a regular expression to find the key and values
$success = preg_match('/^([^(]+)\(([^)]+)\)\.$/', $row, $parts);
// Check the regexp worked
if(!$success) {
echo 'Failed to match row: ' . $row . "\n";
continue;
}
// get the array key from the regexp results
$key = $parts[1];
// the values are all a string, split on the comma to make an array
$values = explode(',', $parts[2]);
// store $key and $values in the result
$result[$key] = $values;
}
// See if it worked
var_dump($result);
Suppose this answer will help you
$text = "
txt_1(val_1,val_2,val_3).
txt_2(val_4,val_5,val_6).
txt_3(val_a,val_b,val_c).
";
$myArry = explode(".", $text);
$resArry = array();
foreach ($myArry as $key => $value) {
if(trim($value)!=""){
$plain = str_replace(array("(",")"),",",$value);
$subArry = explode(",",$plain);
$keyN = explode("(",trim($value));
unset($subArry[array_search($keyN[0],$subArry)]);
unset($subArry[array_search("",$subArry)]);
$resArry[$keyN[0]][]=$subArry;
}
}
echo "<pre/>";
print_r($resArry);
die;
//Output will be like
Array
(
[txt_1] => Array
(
[0] => Array
(
[1] => val_1
[2] => val_2
[3] => val_3
)
)
[txt_2] => Array
(
[0] => Array
(
[1] => val_4
[2] => val_5
[3] => val_6
)
)
[txt_3] => Array
(
[0] => Array
(
[1] => val_a
[2] => val_b
[3] => val_c
)
)
)
$input = 'txt_1(val_1,val_2,val_3).
txt_1(val_4,val_5,val_6).
txt_2(val_7,val_8,val_9).
txt_3(val_10,val_11,val_12).
txt_3(val_13,val_14,val_15).
txt_4(val_16,val_17,val_18).'; // the input string
$temp = explode('.', $input); // seprates from .
$temp = array_filter($temp); // for cutting blank values
$temp = array_map('trim', $temp); // removes newlines
$final = [];
foreach($temp as $val)
{
$key = strtok($val, '('); // search upto token (
$final[$key][] = explode(',' ,strtok(')')); // advance token to )
}
unset($val, $temp); // unset non required things
Here is the output for $final,
Array
(
[txt_1] => Array
(
[0] => Array
(
[0] => val_1
[1] => val_2
[2] => val_3
)
[1] => Array
(
[0] => val_4
[1] => val_5
[2] => val_6
)
)
[txt_2] => Array
(
[0] => Array
(
[0] => val_7
[1] => val_8
[2] => val_9
)
)
[txt_3] => Array
(
[0] => Array
(
[0] => val_10
[1] => val_11
[2] => val_12
)
[1] => Array
(
[0] => val_13
[1] => val_14
[2] => val_15
)
)
[txt_4] => Array
(
[0] => Array
(
[0] => val_16
[1] => val_17
[2] => val_18
)
)
)
Test it here: http://phptester.net/
NOTE: In order to use the short array syntax [] use PHP 5.4
<?php
$text = "
txt_1(val_1,val_2,val_3).
txt_2(val_4,val_5,val_6).
txt_n(val_a,val_b,val_c).
";
$myArray = [];
//You're gonna see why we want to remove this character
//later, it will help us have a cleaner code.
$text = str_replace(')', '', $text);
$arrayGroup = explode('.', $text);
//print_r($arrayGroup);
foreach($arrayGroup as $array) {
$exp = explode('(', $array);
$arrayName = trim($exp[0]);
$arrayValues = explode(',', $exp[1]);
foreach($arrayValues as $value) {
${$arrayName}[] = $value;
}
$myArray[$arrayName] = $$arrayName;
}
echo '<pre>';
print_r($myArray);
echo '</pre>';
echo '<pre>';
print_r($myArray['txt_2']);
echo '</pre>';
After all this, you can use the txt_1 or txt_2 or whatever later, because the variables were created dynamically.
Later in the code you can use $myVar = txt_1[3]; with no problem

How to change the array in following scenario?

I've an SQL query as follows:
SELECT test_user_user_id FROM OCN.tests_users WHERE test_user_test_id = 99
$this->mDb->Query( $sql );
$students_data = $this->mDb->FetchArray();
I get the array as follows:
Array
(
[0] => Array
(
[test_user_user_id] => b9e6493f9f8599bc5a0a5935275228c2
)
[1] => Array
(
[test_user_user_id] => 395599c5891c1418357e2efa89bc3e27
)
[2] => Array
(
[test_user_user_id] => 3255605bb9fd3ecc0295a1bfb3cba147
)
[3] => Array
(
[test_user_user_id] => ebe6711fc156b2cc1a33f64f3d86150f
)
[4] => Array
(
[test_user_user_id] => 627b9c3f21d93f1e13af076cff20b143
)
[5] => Array
(
[test_user_user_id] => 030e96561c01afde1c46384f57cf8749
)
[6] => Array
(
[test_user_user_id] => 9def02e6337b888d6dbe5617a172c18d
)
)
Actually I want the array in following format:
Array
(
[0] => b9e6493f9f8599bc5a0a5935275228c2
[1] => 395599c5891c1418357e2efa89bc3e27
[2] => 3255605bb9fd3ecc0295a1bfb3cba147
[3] => ebe6711fc156b2cc1a33f64f3d86150f
[4] => 627b9c3f21d93f1e13af076cff20b143
[5] => 030e96561c01afde1c46384f57cf8749
[6] => 9def02e6337b888d6dbe5617a172c18d
)
How to get the array in above format?
$resArray = Array();
foreach($myArr as $_arr)
{
$resArray[] = $_arr['test_user_user_id'];
}
You could use array_map:
function user_id($e)
{
return $e['test_user_user_id'];
}
$students_data = array_map('user_id', $students_data);
$new_array = array();
$counter = 0; // optional... because by default starting index is 0... however if you want to change the index then use a counter
foreach($students_data as $row) {
$new_array[$counter] = $row['test_user_user_id'];
$counter++;
}
print_r($new_array);

Group array in php

I have an array, which contains data as follows:
Array (
[0] => Array (
[www.google.com] => www.google.com/a
)
[1] => Array (
[www.google.com] => www.google.com/a
)
[2] => Array (
[www.test.com] => www.test.com
)
[5] => Array (
[www.test.com] => www.test.com/c
)
)
I need to grup all links for particular url like this:
Array (
[www.google.com] => Array (
[0] => www.google.com/a
[1] => www.google.com/a
)
[www.test.com] => Array (
[0] => www.test.com
[1] => www.test.com/c
)
)
Please any help for this?
If we call the first array $domains.
$groups = array();
for ($i = 0; $i <= count($domains); $i++)
{
foreach ($domains[$i] as $domain => $url)
{
$groups[$domain][] = $url;
}
}
print_r($groups);
That might work...

Categories