additing value to array without array_push - php

Hi have the following function to add to an existing array
function push_curr_value($push_this_value, &$list) {
$list[] = $push_this_value;
foreach($list as $value) {
echo $value."<br />";
}
echo sizeof($list);
}
but it does not seem to work. the size of the array $list remains the same.
And calling the function with the following code:
$list = [];
if(array_key_exists('push_value', $_POST)){
push_curr_value($_POST['push_value'], $list];
}

function push_curr_value($push_this_value, &$list) {
$list[] = $push_this_value;
foreach($list as $value) {
echo $value."<br />";
}
echo sizeof($list);
}
public function do_this(){
$num = [1,2,3];
$this->push_curr_value(4,$num);
}
Output:-
1
2
3
4
4

Related

PHP multidimensional array search key

$list[7362][0]['value'] = 'apple';
$list[7362][1]['value'] = 'orange';
$list[9215][0]['value'] = 'lemon';
I want key for value 'orange'. I tried with array_search and array_column, but obviously I have issue array_column.
$key = array_search('orange', array_column($list, 'value'));
as described
PHP multidimensional array search by value
but my case is slighly different. Key should return 7362.
You can try something like this:
<?php
$list = array();
$list[7362][0]['value'] = 'apple';
$list[7362][1]['value'] = 'orange';
$list[9215][0]['value'] = 'lemon';
foreach ($list as $keynum=>$keyarr) {
foreach ($keyarr as $key=>$index) {
if (array_search('orange', $index) !== false) {
echo "orange found in $key >> $keynum";
}
}
}
?>
You can choose to just echo out echo $keynum; for your purpose.
Loop through the arrays and find out where you find orange.
You can refactor that a bit into a function like this:
<?php
function getKeys($list, $text) {
foreach ($list as $keynum=>$keyarr) {
foreach ($keyarr as $key=>$index) {
if (array_search($text, $index) !== false) {
return "$text found in $key >> $keynum";
}
}
}
return "not found";
}
$list = array();
$list[7362][0]['value'] = 'apple';
$list[7362][1]['value'] = 'orange';
$list[9215][0]['value'] = 'lemon';
echo getKeys($list, 'lemon');
?>
echo getKeys($list, 'lemon'); will give you lemon found in 0 >> 9215.
echo getKeys($list, 'orange'); will give you orange found in 1 >> 7362.
echo getKeys($list, 'apple'); will give you apple found in 0 >> 7362.
It's nested to far for the array_column at that level, so just loop:
foreach($list as $k => $v) {
if(in_array('orange', array_column($v, 'value'))) {
$key = $k;
break;
}
}
If there can be more than one then create an array and don't break:
$key[] = $k;
//break;

PHP recursive multidimensional loop

$printArr = recursive($newArray); //calls recursive function
$data = [];
var_dump($data);
var_dump($printArr);
function recursive($array, $level = 0)
{
$searchingValue = 'tableName';
foreach($array as $key => $value)
{
//If $value is an array.
if(is_array($value))
{
recursive($value, $level + 1);
}
else
{
//It is not an array, so print it out.
if($key == $searchingValue)
{
echo "[".$key . "] => " . $value, '<br>';
$data[] = $value;
}
}
}
}
So I have this function and I am trying to save $value value into $data[] array. But it always returns it empty and I don't know why I can't get $value saved outside the function.
If i echo $value I get what i need but like I've mentioned the variables doesn't get saved in this case - table names.
You need to pass the $data to your recursive function. Also you need to return the $data.
Try this code :
function recursive($array, $level = 0, $data =[])
{
$searchingValue = 'tableName';
foreach($array as $key => $value)
{
//If $value is an array.
if(is_array($value))
{
recursive($value, $level + 1 , $data);
}
else
{
//It is not an array, so print it out.
if($key == $searchingValue)
{
echo "[".$key . "] => " . $value, '<br>';
$data[] = $value;
}
}
}
return $data;
}
You can't access variable $data, which is outside the function, from the function. You need to pass it by reference or return it. Small example
<?php
$a = 1;
// Your case
function b() {
$a = 4;
return true;
}
// Passing by reference
function c(&$d) {
$d = 5;
return true;
}
// Using return
function d($d) {
$d = 6;
return $d;
}
b();
var_dump($a);
c($a);
var_dump($a);
$a = d($a);
var_dump($a);
https://3v4l.org/UXFdR

Loop into an array with PHP

I have the following array:
$array = [
['2017-02-26', '2017-02-27'],
['2017-03-01'],
['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'],
['2017-01-05', '2017-01-06', '2017-01-07']
];
I'm looking to loop into this array to have something like this:
// When several dates
From 2017-02-26 to 2017-02-27.
// When only one date
On the 2017-03-01.
What I tried:
foreach ($array as $key => $value) {
$count = count($array[$key]);
if($count==1) {
echo "On the $key[$value]";
}
else {
$first = reset($array);
$last = end($array);
echo "From ".$first." to ".$last.;
}
}
But it doesn't work when there is only one date in the row.
You are looping by foreach() so it will display last echo string .Store result to one variable Eg($display) will be more easy to display that
$display = "";
foreach ($array as $key => $value) {
$count = count($array[$key]);
if($count==1) {
$display .= "On the $value[0] <br>";
}
else {
$first = $value[0];
$last = $value[$count-1];
$display .= "From ".$first." to ".$last."<br>";
}
}
echo $display;
Try this:-
foreach ($array as $key => $value) {
$count = count($value);
if($count==1) {
echo "On the ".$value[0];
}
else {
$first = reset($value);
$last = end($value);
echo "From ".$first." to ".$last;
}
}
Or just copy paste this code, it will work. Your main inside array to play with is $value.

merge many array based on a common key php

I wasn't so clear in my first question, so i deleted it and here is a reformulation;
I have those arrays:
$open = array(array("FAI1","34"),array("FAI2","34"),array("FAI3","34"));
$click = array(array("FAI2","52"),array("FAI1","68"),array("FAI3","99"));
$unsubscribe = array(array("FAI2","103"),array("FAI3","67"),array("FAI1","102"));
$def_sent = array(array("FAI1","34",24),array("FAI2","34",23),array("FAI3","34",27));
$SB = array(array("FAI2","103"),array("FAI3","67"),array("FAI1","102"));
$HB = array(array("FAI2","103"),array("FAI3","67"),array("FAI1","102"));
I searched for a function to merge them and get a result like this:
$result = array(array("FAI1",34,68,102,34,24,102,102)
,array("FAI2","34",23.....),
array("FAI3","34",27....));
and to do this, i used the function, in the php online documentation, and this is the function
function array_merge_recursive() {
$arrays = func_get_args();
$base = array_shift($arrays);
foreach ($arrays as $array) {
reset($base);
while (list($key, $value) = #each($array)) {
if (is_array($value) && #is_array($base[$key])) {
$base[$key] = array_merge_recursive($base[$key], $value);
} else {
$base[$key] = $value;
}
}
}
return $base;
}
But instead of getting the result above i got this:
FAI1|34
FAI2|34
FAI3|34
FAI2|52
FAI1|68
FAI3|99
...
So i need some help to reformulate this function to get the expected result.
Try this function:
function array_merge_rec() {
$arrays = func_get_args();
$result = array();
foreach ($arrays as $arg) {
if (is_array($arg)) {
foreach ($arg as $item) {
if (!isset($result[$item[0]])) {
$result[$item[0]] = $item;
} else {
$result[$item[0]][] = $item[1];
}
}
} else {
echo "$arg skippend because it isn't array\n";
}
}
return array_values($result);
}
Does it help?

implode multidimensional array

while($row=mysql_fetch_assoc($query)) {
$id = $row['id'];
$col1 = $row['name1'];
$col2= $row['name2'];
$col3= $row['name3'];
${"$id"} = array("$col1","$col2","$col3");
${"$s".$i}[] = ${"$id"};
}
This is just a breif example of what i'm trying to accomplish, $i is incremented somewhere else. I'm trying to implode the arrays in the array. So below I have imploded the main array but how do I implode the other arrays?
for($i=0;$i<11;$i++) {
$array = ${"s" . $i};
$outcomes = implode("",$array); //implodes main array
}
Not quite sure what you are trying to achieve here, but does this help?
function recursive_echo ($arr, $spacing = 0) {
$padding = ($spacing) ? str_pad('', $spacing) : '';
foreach ($arr as $key => $val) {
if (is_array($val)) {
echo "{$padding}{$key}:<br />\n";
recursive_echo($val, $spacing + 2);
} else {
echo "{$padding}{$val}<br />\n";
}
}
}
This function just echoes out the data, but it illustrates how to recurse through a multi-dimensional array where the number of dimensions is unknown.

Categories