How to make array construction? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have a problem. I have two arrays and i need to put one into other like this:
array('1'=>1,'2'=>2,'3'=>array());
Can somebody advise how to solve it?

If your first array is
$first = array('1'=>1,'2'=>2)
And the second is
$second = array()
Then just add it in as
$first[] = $second
If the second is
$second[3] = array();
Then you want array_merge
$first = array_merge($first,$second)
There are all sorts of array functions on the left in that link, which come in very handy.

Related

how do I select one word from array list like this one? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this
$words = array("Car", "Dog", "Ship");
now I want to randomly select only one word, please help!
This could work.
$words[rand(0, sizeof($words) - 1)]
You can use array_rand() function to get more than one random index.
array_rand(array, number)
<?php
$words=array("Car", "Dog", "Ship");
$random_keys=array_rand($words,3);
echo $words[$random_keys[0]]."<br>";
echo $words[$random_keys[1]]."<br>";
echo $words[$random_keys[2]];
?>

how to create dynamic string in php like sample? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
hi i want to create string in laravel with foreach and that string must be like this sample:
'item1,item2,item3'
Pay attention to commas and quotation marks.
anyone can help me?
you can use implode function.
Have a look at https://www.php.net/implode
It does exactly that with arrays.
example:
$string = implode(",", $array);

Send array to JSON PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I got these arrays
$address = array("1JzSZFs2DQke2B3S4pBxaNaMzzVZaG4Cqh", "12Cf6nCcRtKERh9cQm3Z29c9MWvQuFSxvT", "1dice6YgEVBf88erBFra9BHf6ZMoyvG88")
$amount= array("100000000","150000000","200000000")
and I want to convert them to json format like below:
{
"1JzSZFs2DQke2B3S4pBxaNaMzzVZaG4Cqh": 100000000,
"12Cf6nCcRtKERh9cQm3Z29c9MWvQuFSxvT": 1500000000,
"1dice6YgEVBf88erBFra9BHf6ZMoyvG88": 200000000
}
I tried too much but couldn't figure out how to do that in PHP, any ideas?
Use array_combine:
echo json_encode(array_combine($address, array_map('intval', $amount)));

Resetting the value of array in contiguous integers using foreach in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Resetting the value of array in contiguous integers(1,2,3,4...) using foreach in PHP
I have a array like(1,3,3,4,5,6)
how can i reset the values like (1,2,3,4,5,6) using foreach loop
$i = 1;
foreach($array as &$item)
$item = $i++;
See here in action.

Add array to array without keys [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I know this must be a huge newbie question, but I didn't find the correct answer. Let's see. I have an array set:
$global_array = array();
Then, I want to add an arry inside $global_array, but I dont want the keys. So, the result I want to get is:
$global_array = array(
array(
'bla',
'bla'
)
);
Sorry for the noob question. Thanks!
This should help:
$global_array[] = array('bla','bla');
All arrays will have keys...
If not defined as default 0,1,2,3...

Categories