php multidimension array values to variables - php

I'm overlooking something but I've tried everything I can think of to get these values into variables.
Existing array is created like this:
$compdata[] = array($currentTime,$indiff,$outdiff,$totaldiff);
And this is the dump of the array:
Array (
[0] => Array ( [0] => 1385955600 [1] => 29749073 [2] => 116376416 [3] => 146125489 )
[1] => Array ( [0] => 1385956200 [1] => 2628480405 [2] => 18073170501 [3] => 20701650906 )
[3] => Array ( [0] => 1385957400 [1] => 2728527955 [2] => 16495107227 [3] => 19223635182 )
)
My question is how to get these values with a foreach or while loop into variables like:
$time = $value[0];
$inbound = $value[1];
$outbound = $value[2];
$total = $value[3];
I know I have to do something to get the nested values. . . .
Much appreciated.

You're literally just missing the loop:
foreach ($compdata as $value) {
$time = $value[0];
$inbound = $value[1];
$outbound = $value[2];
$total = $value[3];
}

You can do this using list, as
foreach ($compdata as $array)
{
list($time,$inbound,$outbound,$total)=$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));
?>

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

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

PHP and 2 multidimensional array compare based on two key values

I have two multidimensional arrays like this:
$original = Array (
[0] => Array
(
[time] => 1364690340
[memberid] => 90
[type] => single
)
[1] => Array
(
[time] => 1364690341
[memberid] => 92
[type] => fixed
)
[2] => Array
(
[time] => 1364690342
[memberid] => 96
[type] => single
)
)
and second one like this
$new = Array (
[0] => Array
(
[time] => 1364825750
[memberid] => 90
[type] => single
)
[1] => Array
(
[time] => 1364825751
[memberid] => 92
[type] => single
)
[2] => Array
(
[time] => 1364825752
[memberid] => 96
[type] => single
)
[3] => Array
(
[time] => 1364825753
[memberid] => 111
[type] => single
)
)
My problem is: I want to search $original array for matches based on memberid and type keys and if memberid and type ARE NOT the same -> I want to remove that array from $original array. So in this case I want to keep [0] Array and [2] Array as in $new array I have same memberid and same type as in original, but I would want to remove [1] Array as memberid is the same, but type is different. So my final $original array will look like this:
$original = Array (
[0] => Array
(
[time] => 1364690340
[memberid] => 90
[type] => single
)
[1] => Array
(
[time] => 1364690342
[memberid] => 96
[type] => single
)
)
Here you go, just tested it and it works as expected.
// Presuming your two arrays are still called $new & $original
$original = array(); // your data
$new = array(); // your data
$newArray = array();
foreach($original AS $key => $val){
$newArray[$val['memberid'] . '-' . $val['type']] = $val;
}
$original = array();
foreach($new AS $key => $val){
if(isset($newArray[$val['memberid'] . '-' . $val['type']])){
$original[] = $newArray[$val['memberid'] . '-' . $val['type']];
}
}
print_r($original);
Without making any assumptions about your data, here's an inefficient solution, O(m * n) if m and n are the lengths of your arrays:
$new_original = array();
foreach ($original as $elem) {
// let's see if $new has something with the same type and memberid
foreach ($new as $candidate) {
if ($candidate['type'] == $elem['type'] &&
$candidate['memberid'] == $elem['memberid']) {
// it does! let's keep $elem
$new_original[] = $elem;
}
}
}
// reassign it to $original if desired
$original = $new_original;
However, it would be much cooler to do more efficient lookups. For example, if we can assume there is at most one element with a given memberid in $new:
// turn $new into a map
$new_as_map = array();
foreach ($new as $candidate) {
$new_as_map[$candidate['memberid']] = $candidate;
}
$new_original = array();
foreach ($original as $elem) {
if (isset($new_as_map[$elem['memberid']])) {
$candidate = $new_as_map[$elem['memberid']];
if ($candidate['type'] == $elem['type']) {
$new_original[] = $elem;
}
}
}
// reassign it to $original if desired
$original = $new_original;

Categories