1
$columns = array( 'title', 'client', 'date', 'product', 'status' );

This is print:

Array
(
    [0] => title
    [1] => client
    [2] => date
    [3] => product
    [4] => status
)

I want to give values for the $columns array to be like this:

Array
(
    [title] => Subject
    [client] => Requester
    [date] => Requested
    [product] => Product
    [status] => Status
)

I have try to make something like this:

$columns['title']       = 'Subject';
$columns['client']      = 'Requester';
$columns['date']        = 'Requested';
$columns['roduct']      = 'Product';
$columns['status']      = 'Status';

But it's not working it merges the two arrays and print this:

    Array
(
    [0] => title
    [1] => client
    [2] => date
    [3] => product
    [4] => status
    [title] => Subject
    [client] => Requester
    [date] => Requested
    [product] => Product
    [status] => Status
)

The code that prints the final result is this:

$columns = array( 'title', 'client', 'date', 'product', 'status' );

$columns['title']       = 'Subject';
$columns['client']      = 'Requester';
$columns['date']        = 'Requested';
$columns['roduct']      = 'Product';
$columns['status']      = 'Status';
John
  • 161
  • 1
  • 13
  • Where is the code you used to generate the last array in your question? – Mark Feb 27 '20 at 11:33
  • You've answered your own question anyway, where you've said "I have try to make something like this:", that is valid PHP that will actually give you the result you want. – Mark Feb 27 '20 at 11:34
  • @MarkOverton Please check the question again I have edited it. – John Feb 27 '20 at 11:36
  • @MarkOverton to understand me more let's say that a function prints the columns and anther function giving them the values (Human Titles) – John Feb 27 '20 at 11:37

4 Answers4

2

You appear to be adding new keys and values to an array that already holds values.

If you want to create a new array with values from one as keys, and values from another as the values, use array_combine.

You can also just re-assign to your original array.

<?php

$columns = array( 'title', 'client', 'date', 'product', 'status' );
$values  = array('Subject', 'Requestor', 'Requested', 'Product', 'Status');

$result  = array_combine($columns, $values);

var_export($result);

Output:

array (
'title' => 'Subject',
'client' => 'Requestor',
'date' => 'Requested',
'product' => 'Product',
'status' => 'Status',
)

To turn values into keys, you can use array_flip.

Progrock
  • 7,373
  • 1
  • 19
  • 25
0

If i understood you correctly you want to add these data

$data = ['Subject', 'Requester', 'Requested', 'Product', 'Status'];

to the columns name that you mention

$columns = array( 'title', 'client', 'date', 'product', 'status' );

so it would be something like this one

$columns = array( 'title', 'client', 'date', 'product', 'status' );


function prepareArray($columns){

    $humansTitles = [
        'title' => 'Subject',
        'client' => 'Requester',
        'date' => 'Requested',
        'product' => 'Product',
        'status' => 'Status'
    ];

    $res = [];

    foreach($columns as $col){
        $res[$col] = $humansTitles[$col];
    }

    return $res;
}

print_r(prepareArray($columns));
Joseph
  • 5,644
  • 3
  • 18
  • 44
  • Yes, you now understand me and your code working well but there's one problem that the columns order is not static and may be changed, for example: we must check if it's 'title' then give it a value 'Subject' are you understand me? – John Feb 27 '20 at 11:47
  • could you gave an example ? – Joseph Feb 27 '20 at 11:48
  • The $columns order may be changed it may be like this: data, product, title, status, client the $coumns order is not static. – John Feb 27 '20 at 11:50
  • maybe we could sort it in alpha order? – Joseph Feb 27 '20 at 11:51
  • So if this happens it will print wrong data, for example, data will print "Subject" and the product will print "Requester" etc.. – John Feb 27 '20 at 11:51
  • No, we must make it checking for the name of the key and depending on it giving it the correct value. Like title = Subject, client = Requested. – John Feb 27 '20 at 11:53
  • there is something i miss understand as i know the columns name is stattic and didn't change only the values that is changed so you can add the `columns` array inside the function that no one will change its order – Joseph Feb 27 '20 at 11:53
  • you can't make this check because your values array is not associative – Joseph Feb 27 '20 at 11:55
  • So what's the best solution if the $columns array I got them from the database? Please check this image to understand how my systems work https://i.imgur.com/RoPHhyA.png it's like this and when the user saves it's order the order is saved in the database. – John Feb 27 '20 at 11:59
  • ok, it is clear now but the last question how your data looks like that it will be attached to these columns? – Joseph Feb 27 '20 at 12:01
  • I will get the order that the user have chosen from the database and using 'foreach' to print this in the table 'thead' tag but before this, I need to give this order the human titles. Are you understand me? – John Feb 27 '20 at 12:04
  • aha, i got that let me fix my answer – Joseph Feb 27 '20 at 12:06
  • 1
    Thank you, you are amazing! – John Feb 27 '20 at 12:15
  • here much simpler .. array_values && array_combine – Salines Feb 27 '20 at 12:35
  • @Salines it wouldn't work correctly here because it's columns comes in different order depends on the user order so it wouldn't work here, please remove the downvote otherwise edit the answer if you had another efficient way – Joseph Feb 27 '20 at 12:45
-1
$columns = [];
$columns['title']       = 'Subject';
$columns['client']      = 'Requester';
$columns['date']        = 'Requested';
$columns['roduct']      = 'Product';
$columns['status']      = 'Status';
print_r($columns);
Yasin
  • 124
  • 2
  • 11
-1

You want an associative array

An associative array is an array that has associative keys, e.g. strings for keys.

There are multiple ways of creating these, here is one way:

$columns = [];
$columns['title'] = 'Subject';
$columns['client'] = 'Requester';
$columns['date'] = 'Requested';
$columns['product'] = 'Product';
$columns['status'] = 'Status';

Alternatives:

The short-hand for creating an array is using square brackets:

$columns = [
    'title' => 'Subject', 
    'client' => 'Requester', 
    'date' => 'Requested', 
    'product' => 'Product', 
    'status' => 'Status'
];

This will do the exact same thing but you reference an array using array() rather than []:

$columns = array(
    'title' => 'Subject', 
    'client' => 'Requester', 
    'date' => 'Requested', 
    'product' => 'Product', 
    'status' => 'Status'
);
Mark
  • 1,852
  • 3
  • 18
  • 31
  • You still don't understand me and I know it's my wrong let me be more clear, I have a function that gets the order of the columns of a data table from the user option (Database) and the anther functions get these order columns and giving them a value to print this order columns in the front end with a human titles. I hope now you understand me. – John Feb 27 '20 at 11:42
  • Please edit your question and include this function. – Mark Feb 27 '20 at 11:43