logo
down
shadow

Create an array with associative array keys and numeric array values PHP


Create an array with associative array keys and numeric array values PHP

By : Márton Kovács
Date : November 17 2020, 03:01 PM
will be helpful for those in need Use array_keys instead of array_flip like so:
code :
$array1 = ["fname" => "NULL", "lname" => "NULL", "id" => "NULL"];

$array2 = ["john", "smith", "11123"];

$new_array = array_combine(array_keys($array1), $array2);

print_r($new_array);
Array
(
    [fname] => john
    [lname] => smith
    [id] => 11123
)


Share : facebook icon twitter icon
merge two arrays with numeric keys for values of associative array to insert in MySQL in multiple rows

merge two arrays with numeric keys for values of associative array to insert in MySQL in multiple rows


By : Gyan
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I'm getting two arrays $post_img & $post_sort resulting: , Use array:
code :
$newarray = array('Image' => $post_img, 'Sort' => $post_sort);
$sql = "INSERT INTO tablename SET Image=:image, Sort=:sort";
$dbImgInsert = $db->prepare($sql); // preparing sql for insert, using parameters

$c = count($post_img)-1;

for ($i=0;$i<=$c;$i++;) { // looping through the arrays

    $dbImgInsert->execute(array(   // inserting the data into the prepared query and into the db
        'image' => $post_img[$i],
        'sort' => $post_sort[$i]
        ));
} // for
$sql="";
$c = count($post_img)-1;
for ($i=0;$i<=$c;$i++;) { // looping through the arrays

    $sql.="('{$post_img[$i]}', '{$post_sort[$i]}'), ";
}
$sql ="INSERT INTO tablename (Image, Sort) VALUES " . trim($sql,',');
Replace numeric array keys with associative keys from array

Replace numeric array keys with associative keys from array


By : Lotus
Date : March 29 2020, 07:55 AM
it fixes the issue I have a dataset similar to this in which I am trying to replace the numeric key values within DATA to the corresponding values in COLUMNS. I can do this in a loop but I don't think I'm doing it in the most efficient way possible. Can anyone suggest any nice functions that I haven't considered to accomplish this?
code :
foreach ($data->DATA as $key => $array) {
    $data->DATA[$key] = array_combine($data->COLUMNS, $array);
}
php re-index numeric keys in associative array with combined numeric and string keys

php re-index numeric keys in associative array with combined numeric and string keys


By : Kiran Kumar
Date : March 29 2020, 07:55 AM
it should still fix some issue I have this array , If your array isn't very large, you could build another array:
code :
$output = [];

foreach ($original as $key => $item):
  if(is_string($key)) $output[$key] = $item;
  else $output[] = $item;
endforeach;
array_unshift($original,'dummy');
array_shift($original);
How to replace numeric keys in associative array with their corresponding values

How to replace numeric keys in associative array with their corresponding values


By : Anonimus555
Date : March 29 2020, 07:55 AM
seems to work fine I do have following associative multidimensional array available after json_decode(string, true). , You can do like this
code :
foreach($arr["data"] as $key=>&$data){
  $new = array();
  foreach($data as $key2=>$value){
    $new[$value] = $value;
  }
  $data = $new;
}
print_r($arr);
Can PHP's array_combine be used to create an associative array with (sparse) numeric keys?

Can PHP's array_combine be used to create an associative array with (sparse) numeric keys?


By : Will_kc
Date : March 29 2020, 07:55 AM
seems to work fine I use a preg_match to find keys and values in a webpage. Some of the keys are numbers, like 101357, but most are strings. When I use array_combine to create an array with those keys and values, it converts the keys that are numbers into ordered array elements starting at key 0: , I don't get the same results as you:
Related Posts Related Posts :
  • How to add new Custom Title in Woocommerce My Account sidebar?
  • Mattermost, slash command response as message attachment
  • Library failure when installing Libsodium (Amazon EC2)
  • Page footer centering using MPDF
  • HTML : Different link at each click
  • TCPDF how to prevent page break within a block (split content)?
  • How to insert values in table
  • Php query builder for OR and AND condition
  • ZF2 Ajax call returns all information instead of just the id being sent
  • Order custom post types in WordPress by the meta_value of another plugin
  • saving bank row using array
  • Convert URL with query parameters to guzzle
  • Get POST values by index in codeigniter
  • Composer is not functioning?
  • Retrieving an Array with Axios in Laravel
  • How to get the last child of each cd element?
  • How to allow users to connect their own domain
  • PHP multidimensionnal array and empty value
  • PHP if statement doesn't work properly (paradoxical)
  • BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::searchable()
  • PHP explode() returns array inside an array
  • Echo parse error for creating new php file
  • PHP Parse error: syntax error, unexpected '?'
  • PHP Session not working correctly on a particular host
  • laravel how to get the extremities tables through the pivot
  • Show duplicate value in array only one but show all others
  • SMTP Emails not send (There is something wrong with server config as code is fine for other servers)
  • I can't turn off debug bar in Laravel on production
  • PHP creating multidimensional json
  • Increment value with get
  • Laravel 5.5: Convert query builder to eloquent
  • Reverse proxy onto a WordPress page?
  • Jumble Solver url rewrite
  • Using .htaccess to Execute php if directory of the same name not found
  • mysql - Unknown Column In Where Clause
  • Receive Emails into my Admin panel with php or other
  • PHP - adding only distinct value to an array in foreach loop not working
  • How to pass date picker from one page to another
  • Try catch always return error when I insert 2 data
  • Php redirect not working on submission
  • How to get array value from wordpress database get_results
  • how Automatically go from first.php to second.php
  • uploading file not working in codeigniter 3 error: you did not select any file
  • Yii2, gridview can not show some html tags inside cells
  • What are the thing we need to configure to implement live notification module (using redis and socket.io) in https serve
  • How to handle multi criterias search in PHP
  • PHP - Get maximum key and its value in array
  • PHP: how to retrieve all the metrics and dimensions from the Google Metadata API?
  • Deliver php mails to special-character-email adresses, such as äüö
  • Delete a single row from an MSSQL table from PHP
  • Property [name] does not exist on this collection instance
  • Pass input value through PHP parameter
  • Running two PHP versions on the same server (STRETCH)
  • Checking if a file has been selected for upload
  • Model not updating laravel
  • How to add a description of words from text to an array, to an array key by passing words?
  • Nginx rewrite rule not not passing variables if "/" is at the end of the url
  • Modify decimal point and thousands separator without changing the number of decimals
  • Remove PHP errors from CLI and PHPMyAdmin
  • Yii2 rest api - Url manager add parameters
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org