how to assign elements of one array to another array in php - php

i retrieved data from mysql table in php
$array2=array(); while($q1= mysql_fetch_assoc($result))
{ print_r($q1);
$array2[]= $q['user_id'];
}
the print_r($q1) will output this Array ( [user_id] => 1 ) Array ( [user_id] => 2 ) Array ( [user_id] => 4 ) .
Now i want to assign these values into another array i.e
$array2[]=$q['user_id']; but when i echo $array2 this gives me result otherthan $print_r($q) i.e
Array ( [0] => [1] => [2] => )
my question is how can i get the value of [user_id]=>4 from this array

$q is different from $q1..
You need:
$array2[]= $q1['user_id'];
It should actually be throwing a warning about $q['user_id'] not being defined. Do you have error reporting turned on?

Related

I was trying to print the value of count in my data variable in laravel, the following is the value of the variable

print_r($data) gives the below value, I am not sure about how to get the count value from the below
Array ( [result] => Array ( [0] => Array ( [_id] => [count] => 6 ) ) [ok] => 1 )
I want to get 6 as output
You may explictly access the array using its variable name then the keys
For example:
$data['result'][0]['count']

array index changing into string [name] not number

I have form (display.php) that will get multiple selected option from user. Then this selected option will be formatted to another page (page.php). The problem occur when I try to display those multiple selected option. The array index are changing into string [name]!
Array ( [0] => 3204120006 [1] => 3204120011 [2] => 3204120010 [3] => 3204120009 )
Array ( [name] => BIRU ) Array ( [name] => BOJONG ) Array ( [name] => MAJAKERTA ) Array ( [name] => MAJALAYA )
Here the code of above display.
<?php
if (isset($_POST["desas"])) {
$ddes=$_POST["desas"];
print_r ($ddes);
foreach ( $ddes as $iddesa ) {
$namadesa=mysql_query("SELECT name FROM villages WHERE id='$iddesa' ");
if ($namadesa) {
$datadesa = mysql_fetch_assoc($namadesa);
print_r($datadesa);
}
} else
$datadesa="";
}
?>
My question is how to change ([name]=>BIRU),([name]=>BOJONG) into index ([0]=>BIRU),([1]=>BOJONG) etc on those array? or something missing in mysql fetch?
you can use array_values. reference: https://secure.php.net/manual/en/function.array-values.php
$datadesa = array_values(array_values);

Sort array based on key value

I'm using a form where the pair of inputs can be added per automatic. One store value in select and the other as input
After submit I receive the values in array and I need to associate them together. So all key values [0] belongs together and all [1] and so on.
Array
(
[issue] => Array
(
[0] => 2
[1] => 3
)
[qty] => Array
(
[0] => 1
[1] => 2
)
)
How can I do this using PHP?
Just use a simple foreach loop.
$combined = array();
foreach ($_POST["issue"] as $k=>$v) {
$combined[$k] = array($_POST["issue"][$k], $_POST["qty"][$k]);
}
print_r($combined);

Convert multiple-dimensional array in to one dimensional array by summing the vaues like shown here

Array
(
[0] => Array
(
[0] => 17
[1] => 111
)
[1] => Array
(
[0] => 17
[1] => 10
)
[2] => Array
(
[0] => 20
[1] => 111
)
)
I want to convert this array as:
array
(
[17]=>121
[20]=>111
)
is there any php array function which can do it easily. I know the other way, but want to know if any ready made function can do that or not??
Please Help.
Here I actually wanted to convert
[0] => Array
(
[0] => 17
[1] => 111
)
17 to key and take 111 as value then in next array
[1] => Array
(
[0] => 17
[1] => 10
)
do the same thing get first value 17 as key and add 10 into previous value 111
which is 121 and then
[2] => Array
(
[0] => 20
[1] => 111
)
take 20 as key and assign value 111 to that key so basically, I want first value as a key and second value as value and for all same keys I want to add values as I stated before.
I thought there might be any PHP ready-made function for that as I have seen there are lots of array processing functions available in PHP. Now, I realized there is no any such kind of function available. I can do my own custom code for this purpose but was looking for good logical solution.
No builtin function for that but it is a simple foreach loop. Assume your array is stored in variable $arr;
$return = array();
foreach ($arr as $a) {
$return[$a[0]] += $a[1];
}
echo "<pre>"; print_r($return);
if you are calling multiple times then you can easily write your own function
$arr[0]= array(17,111);
$arr[1]= array(17,10);
$arr[2]= array(20,111);
$return = subArr($arr);
echo "<pre>"; print_r($return);
function subArr($arr) {
$result = array();
foreach ($arr as $a) {
$result[$a[0]] += $a[1];
}
return $result;
}

show random values from array of array

Below is a array generated by a query builder.
$random_array = Array ( [0] => Array ( [text] => A great time was had by all! )
[1] => Array ( [text] => KILL SHOT )
[2] => Array ( [text] => How is it possible)
[3] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g )
[4] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g )
)
Currently i am doing like this to print the random value
print_r(array_rand($random_array,1));
This is printing the array key as 3 or 1 etc(random from above array).I want to print the value of the key and not the key.
e.g I want to print the random value like this "http://www.youtube.com/watch?v=KwGOZpbxU9g" or "A great time was had by all!" instead of 3 or 1 which is printing now.
Is it possible to do this.
You will have one more line of code as shown below:
$array_key = array_rand($random_array,1); //get the key
print_r( $random_array[$array_key] ); //use the key to print value
What about simply calling
$randNumber = rand(0,count($random_array))-1; //index in array starts with 0
print (string) $random_array[$randNumber];

Categories